目前,我有這樣的代碼通過一個查詢的整個結果循環:PHP迭代通過一個MySQL造成了一定的時間
$result = mysqli_query($con, $sql);
$data = array();
while($row = mysqli_fetch_array($result)) {
$name = $row ['name'];
$desc = $row ['description'];
$cat = $row ['category'];
$price = $row ['price'];
$quantity = $row ['quantity'];
$id = $row ['productID'];
$image = $row ['image'];
$arr = array();
$arr ["id"] = $id;
$arr ["name"] = $name;
$arr ["desc"] = $desc;
$arr ["cat"] = $cat;
$arr ["price"] = $price;
$arr ["quantity"] = $quantity;
$arr ["image"] = $image;
array_push($data, $arr);
}
echo json_encode($data);
我想通過一個設定的這一改變能夠環結果。我有一個名爲$amount
的代碼,它是我想要的結果數量。
我聽說我應該使用mysql_result()
和for循環來計數到$amount
,但我不知道我的現有代碼將如何使用該設置!建議請。
編輯:
@Styphon指出了,我實際上並不需要改變我的迭代代碼爲此得到解決。只需要添加一個LIMIT
到我的SQL查詢。
爲什麼不只是爲查詢添加限制?爲什麼你把行放入一個var,然後將var放入數組,然後推入數據? – Styphon
我還沒有太多的MySQL經驗,這將如何完成?聽起來它可以工作,但! – benharris
https://dev.mysql.com/doc/refman/5.0/en/select.html尋找LIMIT – Pakspul