OO Mysqli PHP 数据显示不正确

OO Mysqli PHP data isn't displaying correctly

我正在尝试使用 while 循环在 table 中显示信息,但如果只有 1 个结果,table 将不会显示任何内容。如果超过 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;