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