使用php在列中显示2条记录

时间:2016-10-04 21:08:51

标签: php

所以我有一个代码

<?php
$showorder = "SELECT order_number FROM orders WHERE customer_number=522";
$orderesult = mysqli_query($con, $showorder);
$ord = mysqli_fetch_array($orderesult);
?>

在我的数据库中,客户编号522有2个订单编号,当我试图显示结果时,它只显示1。

这是我的其他代码

echo "<table>";
echo "<th>Order Number</th><th>Order date</th>";
echo "<tr><td>";
echo $ord["order_number"];
echo "</td><td>";
echo $ord["order_date"];
echo "</td></tr>";

6 个答案:

答案 0 :(得分:1)

您只需在此处使用while()获取所有记录,例如:

while($ord = mysqli_fetch_array($orderesult)){
   //echo all value here
}

另请注意,如果您要打印$ord["order_date"],则还需要在查询中选择列。

否则,$ord只会包含order_number值。

答案 1 :(得分:0)

您的SQL缺少额外的列。

当前SQL:

SELECT order_number FROM orders WHERE customer_number=522

更改为:

SELECT order_number, order_date FROM orders WHERE customer_number=522

答案 2 :(得分:0)

 echo "<table>";
 echo "<th>Order Number</th>";

 while($ord = mysqli_fetch_array($orderesult)) {

   echo "<tr><td>";
   echo $ord["order_number"];

   echo "</td></tr>";

 }

答案 3 :(得分:0)

你必须使用循环来显示所有结果,并且你可以使用echo一次。

while($ord = mysqli_fetch_array($orderesult)) {
    echo "<table>
    <th>Order Number</th><th>Order date</th>
    <tr><td>".
    $ord["order_number"]."
    </td></tr>";
}

答案 4 :(得分:0)

放入mysqli_fetch_array($ orderesult);在一个循环中。



    while($ord = mysqli_fetch_array($orderesult)) {
        echo $ord["order_number"];
        # code
    }

答案 5 :(得分:0)

使用以下代码替换您的代码,然后重试

<?php
    $showorder = "SELECT order_number, order_date FROM orders WHERE customer_number=522";
    $orderesult = mysqli_query($con, $showorder);

    echo "<table>";
    echo "<tr>";
    echo "<th>Order Number</th><th>Order date</th>";
    echo "</tr>";
    while($ord = mysqli_fetch_array($orderesult)) {
        echo "<tr>";
        echo "<td>$ord['order_number']</td>";
        echo "<td>$ord['order_date']</td>";
        echo "</tr>";
    }
    echo "</table>";
?>