doushan1850 2015-04-12 15:40
浏览 8
已采纳

OO Mysqli PHP数据无法正确显示

I'm trying to display information in a table using a while loop but the table wont display anything if there is only 1 result. If there are more than 1, it will display the results -1. For example, for 5 results it will only display 5.

My query is:

$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;

and my table is:

<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>

Does anyone know what I'm doing wrong?

  • 写回答

1条回答 默认 最新

  • dongpai2468 2015-04-12 16:13
    关注

    Your issue is that you are calling ->fetch_assoc(); on your result set, before your loop, so when you get to the loop, your internal pointer is at the 2nd returned row. You need to remove $rowIndexInvoice = $resultIndexInvoice->fetch_assoc();

    $resultIndexInvoice = $connect_db->query($queryIndexInvoice);
    $rowIndexInvoice = $resultIndexInvoice->fetch_assoc(); <--Remove this line
    $numIndexInvoice = $resultIndexInvoice->num_rows;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?