PDO - 在表中仅显示 1 个结果,并非所有结果都具有相同的 ID

我目前在我的 MySQL 数据库中有 2 个结果,user_id我想在我的 HTMLtable中用回显所有PDO结果,但它每次只显示 1 个结果,而不是全部。


<?php

$querytest = "SELECT o.output_valu,

  p.amount,

  p.amount_all,

  p.order_id,

  p.datetime

FROM allusers a

INNER JOIN order_history o

  ON a.account_number = o.account_number

INNER JOIN paymentall p

  ON o.output_vl_id = p.output_vl_id

WHERE a.account_number = :account_num

ORDER BY p.datetime";

$statementtest = $conn->prepare($queryoutgo);

$statementtest->bindParam(':account_num', $account_num);

$statementtest->execute();

$test_result = $statementtest->fetchAll();

foreach ($test_result as $row) {

  $outputtest = $row['output_valu']; 

}    

?>   

<table>

  <tr>

    <th>Test</th>

  </tr>

  <tr>

    <td><?php echo $outputtest; ?></td>

  </tr>

</table>

它print_r($test_result);显示了我的 2 resultsin array,但为什么不使用我的代码呢?我过去一直与mysqli非 PDO 合作,也许这里有人可以帮助我:)


斯蒂芬大帝
浏览 98回答 1
1回答

红糖糍粑

在您的foreach块中,您会覆盖$outputtest每次迭代。这意味着只会显示最后一个结果。根据您是要在单独的行上显示每个结果还是要将所有结果放在一个单元格中,您应该在 中创建单元格foreach或将所有结果连接在一起。编辑:我认为你想要的是:$querytest = "SELECT o.output_valu, p.amount, p.amount_all, " .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"p.order_id, p.datetime " .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"FROM allusers a inner join order_history o " .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"ON a.account_number = o.account_number " .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"INNER JOIN paymentall p " .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"ON o.output_vl_id = p.output_vl_id " .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"WHERE a.account_number =:account_num " .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"ORDER BY p.datetime ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$statementtest = $conn->prepare($queryoutgo);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$statementtest->bindParam(':account_num', $account_num);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$statementtest->execute();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$test_result = $statementtest->fetchAll();?><table>&nbsp; <tr>&nbsp; &nbsp; <th>Test</th>&nbsp; </tr>&nbsp; <?php foreach($test_result as $row) { ?>&nbsp; &nbsp; <tr><td><?= $row['output_valu']; ?></td></tr>&nbsp; <?php } ?></table>
打开App,查看更多内容
随时随地看视频慕课网APP