1
的列设置像这样在一个名为表“产品”生成JSON,只有通过我的产品表中获取最后一行
+------------++---------------++----------------------++---------------++----------------+
| product_id | product_name | product_description | product_image | product_price |
+------------++---------------++----------------------++---------------++----------------+
| 1 | a1 | good | url.jpg | 150dkk |
+------------++---------------++----------------------++---------------++----------------+
| 2 | a2 | nice | url.jpg | 150dkk |
+------------++---------------++----------------------++---------------++----------------+
当年这里是我的代码,所以我环路和将它们推入一个将成为JSON的数组中。
//Create an array
$json_response = json_decode('{"products":[]}');
$sql = "SELECT * FROM products";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$row_array['id'] = $row['product_id'];
$row_array['name'] = $row['product_name'];
$row_array['description'] = $row['product_description'];
$row_array['image'] = $row['product_image'];
$row_array['price'] = $row['product_price'];
}
// Push the columns into the created array
array_push($json_response->products, $row_array);
// Encode into an object
$oJsonResponse = json_encode($json_response);
// Save it in a new file that holds the json from MySQL
file_put_contents('products.json', $oJsonResponse);
的问题是,我只是设法让与的2号产品进入我的JSON文件,因此在第一个产品永远不会保存在以.json文件的。当我做echo = "$row[product_name]";
- 我得到这两个产品
当然!非常感谢你为我指出了这一点,我在几分钟内接受了你的回答:) – SmalliSax 2014-11-14 18:08:10