gpt4 book ai didi

php - OO Mysqli PHP 数据无法正确显示

转载 作者:行者123 更新时间:2023-11-29 07:39:08 24 4
gpt4 key购买 nike

我尝试使用 while 循环在表中显示信息,但如果只有 1 个结果,表将不会显示任何内容。如果超过1个,则显示结果-1。例如,对于 5 个结果,它将仅显示 5 个。

我的查询是:

$queryIndexInvoice = 
"SELECT *
FROM invoices, clients, users
WHERE invoices.user_id = users.id
AND invoices.client_id = clients.id
AND invoices.estimate = 0
AND invoices.user_id = '$user_id'
AND deleted = 0
ORDER BY invoices.id
DESC LIMIT 5";

$resultIndexInvoice = $connect_db->query($queryIndexInvoice);
$rowIndexInvoice = $resultIndexInvoice->fetch_assoc();
$numIndexInvoice = $resultIndexInvoice->num_rows;

我的 table 是:

<tbody>
<?php while ($IndexInvoice = $resultIndexInvoice->fetch_assoc()) {?>
<tr class='table_items'>
<td class='item_strip'></td>
<th><input type='checkbox'></th>
<td><?= $IndexEstimate['invoice_id'] ?></td>
<td><?= $dateIndexEstimate ?></td>
<td><?= $IndexEstimate['client_first']?> <?= $IndexEstimate['client_last']?></td>
<td><?= $IndexEstimate['total'] ?></td>
</tr>
<?php
}
?>
</tbody>

有人知道我做错了什么吗?

最佳答案

您的问题是您在循环之前对结果集调用 ->fetch_assoc(); ,因此当您进入循环时,您的内部指针位于第二个返回行。您需要删除 $rowIndexInvoice = $resultIndexInvoice->fetch_assoc();

$resultIndexInvoice = $connect_db->query($queryIndexInvoice);
$rowIndexInvoice = $resultIndexInvoice->fetch_assoc(); <--Remove this line
$numIndexInvoice = $resultIndexInvoice->num_rows;

关于php - OO Mysqli PHP 数据无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29591256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com