kelvin
kelvin

Reputation: 3

Displaying 2 records in a column using php

So I have a code

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

in my database customer number 522 has 2 order numbers, when i tried to show the result, it only shows 1.

Here's my other code

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

Upvotes: 0

Views: 53

Answers (6)

Mo Shal
Mo Shal

Reputation: 1637

you must use loop to show all result , and you can use echo one time .

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

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133360

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

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

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

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

 }

Upvotes: 0

Sathvik Chinnu
Sathvik Chinnu

Reputation: 575

Replace your code with the below code and then try again

<?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>";
?>

Upvotes: 0

Maco
Maco

Reputation: 21

Put mysqli_fetch_array($orderesult); in a while loop.



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

Upvotes: 0

devpro
devpro

Reputation: 16117

You just need to use while() here for getting all records, something like:

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

Also note that, if you want to print $ord["order_date"] than you must need to select column also in your query.

Otherwise, $ord will only contain order_number value.

Upvotes: 1

Jack
Jack

Reputation: 3405

Your SQL is missing the extra column.

Current SQL:

SELECT order_number FROM orders WHERE customer_number=522

Change to:

SELECT order_number, order_date FROM orders WHERE customer_number=522

Upvotes: 0

Related Questions