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"] ]