Rumah > Soal Jawab > teks badan
P粉1514660812023-08-23 12:44:56
Beginilah cara saya mencipta suapan JSON:
$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
Hanya buat tatasusunan daripada hasil pertanyaan dan kodkannya
$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);
Hasil keluaran adalah seperti berikut:
[ {"id":"31","name":"product_name1","price":"98"}, {"id":"30","name":"product_name2","price":"23"} ]
Jika anda mahukan gaya lain, anda boleh menukar fetch_assoc() kepada fetch_row() dan dapatkan output berikut:
[ ["31","product_name1","98"], ["30","product_name2","23"] ]