我有一个MySQL服务器,我想用PHP指令从数据库中抓取X行。以下是我的命令和数据库信息(quote = "Q-1-Rev04"):
$quoteLineRequest = "SELECT `id`, `quantity`, `unitprice`, `goodsref` FROM `quoteline`
WHERE `quote` = '".$quoteid."'";
if($result = mysqli_query($link, $quoteLineRequest)){
if (mysqli_num_rows($result) > 0){
$quoteLines = mysqli_fetch_assoc($result);
var_dump($quoteLines);
}
}
数据库值但是当我var_dump $quoteLines时,它只给我4个结果,而不是预期的12个:
array(4) { ["id"]=> string(3) "113" ["quantity"]=> string(4) "1100" ["unitprice"]=> string(4) "0.26" ["goodsref"]=> string(8) "100-0011" }
为什么不抓取所有对应的值?
这是因为mysqli_fetch_assoc
只从您的查询结果中获取一行。
如果您想获取所有的行,您需要使用fetch_all
:
if (mysqli_num_rows($result) > 0) {
$quoteLines = fetch_all($result, MYSQLI_ASSOC);
var_dump($quoteLines);
}