P粉1514660812023-08-23 12:44:56
這是我創建JSON feed的方法:
$mysqli = new mysqli('localhost', 'user', 'password', 'myDatabaseName'); $myArray = array(); if ($result = $mysqli->query("SELECT * FROM phase1")) { $tempArray = array(); while ($row = $result->fetch_object()) { $tempArray = $row; array_push($myArray, $tempArray); } echo json_encode($myArray); } $result->close(); $mysqli->close();
P粉0445262172023-08-23 09:42:01
只需從查詢結果建立一個數組,然後對其進行編碼
$mysqli = new mysqli('localhost','user','password','myDatabaseName'); $myArray = array(); $result = $mysqli->query("SELECT * FROM phase1"); while($row = $result->fetch_assoc()) { $myArray[] = $row; } echo json_encode($myArray);
輸出結果如下:
[ {"id":"31","name":"product_name1","price":"98"}, {"id":"30","name":"product_name2","price":"23"} ]
如果你想要另一種樣式,可以將fetch_assoc()改為fetch_row(),得到以下輸出:
[ ["31","product_name1","98"], ["30","product_name2","23"] ]