Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php Fetch Results into the table

Below are my codes, However, I didn't get a table as I expect. Rather than the first row only seeming to be ok, Other Rows are not coming in the table. Please refer to the photo.

    $Sql = "SELECT Registration_Number,Name FROM vta";
    
    $Result = $conn->Query($Sql);
    
    
    if ($Result -> num_rows> 0) {    
        echo "<table border=2> 
            <tr><th>Registration_Number</th>
            <th>Name</th></tr>";
    
        while ($Row = $Result->fetch_assoc()) {
            // $Rm=$Row['Registration_Number'];
            // $Nm = $Row['Name'];
    
            echo "<tr><td>$Row[Registration_Number]</td><td>$Row[Name]</td></tr></table>";
        }
    }
    
    $conn->Close();

enter image description here

like image 862
A.t Avatar asked Feb 17 '26 07:02

A.t


1 Answers

You have to move the closing </table> outside the loop.

$Sql = "SELECT Registration_Number,Name FROM vta";

$Result = $conn->Query($Sql);


if($Result->num_rows > 0) {
    echo "<table border=2> 
        <tr><th>Registration_Number</th>
        <th>Name</th></tr>";

    while ($Row = $Result->fetch_assoc()){
        echo "<tr><td>$Row['Registration_Number']</td><td>$Row['Name']</td></tr>";
    }

    echo '</table>';
}

$conn->Close();
like image 106
nito Avatar answered Feb 19 '26 23:02

nito



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!