表使用方法:生成PHP陣列對JS
--------------------
Product | Price
--------------------
apples | 5
oranges | 6
peaches | 7
如何從表中獲取行,並生成一個JSON陣列?
表使用方法:生成PHP陣列對JS
--------------------
Product | Price
--------------------
apples | 5
oranges | 6
peaches | 7
如何從表中獲取行,並生成一個JSON陣列?
喜只需fetch
從數據table
並使用json_encode
$con = mysqli_connect('localhost','root','password','yourdatabase') or die("Could not connect database");
$result = mysqli_query($con,"select * from youtable");
$arr = array();
while($row = mysqli_fetch_assoc($result)){
$arr['myproducts'][] = $row;
}
$js_array = json_encode($arr);
echo $js_array;
?>
輸出
{"myproducts":[{"price":"4","products":"apples"},{"price":"5","products":"oranges"}]}
你應該簡單只是做SQL SELECT
,然後它的結果應該是json_encode()
例子:
SELECT * FROM `table_name`
Afther這在你的代碼:
while($r = $result->fetch_assoc()) {
$newArr[] = $r;
}
echo json_encode($newArr);
http://stackoverflow.com/help/how-to-ask。 –
什麼是'js數組'? – Saty
所以你想讓我們爲你寫一切嗎?從MySQL查詢 - > PHP - >達到JavaScript? –