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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么