我尝试使用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;
我的表是:
<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>
有谁知道我做错了什么?
答案 0 :(得分:2)
您的问题是您在循环之前在结果集上调用->fetch_assoc();
,因此当您进入循环时,您的内部指针位于第二个返回的行。您需要删除$rowIndexInvoice = $resultIndexInvoice->fetch_assoc();
$resultIndexInvoice = $connect_db->query($queryIndexInvoice);
$rowIndexInvoice = $resultIndexInvoice->fetch_assoc(); <--Remove this line
$numIndexInvoice = $resultIndexInvoice->num_rows;