Hello, I'm not able to display the whole select result information in the table and I have no idea what I am doing wrong, I would appreciate your help so much.

So here's my code:

<?php
$DBServer = 'localhost';
$DBUser = 'root';
$DBPass = '';
$DBName = 'world';
$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

$sql = "SELECT Id, Name, CountryCode, District, Population FROM city WHERE CountryCode = 'USA'";
$result = $conn->query($sql);

if($result->num_rows > 0){
    echo "<table border='1'> <tr> <th>Id</th> <th>Name</th> <th>Code</th> <th>District</th> <th>Population</th> </tr>";
            while($row = mysqli_fetch_array($result)){
                echo"
                <tr> <td>".$row['Id'] ."</td> <td>".$row['Name'] ."</td> <td>".$row['CountryCode'] ."</td> <td>".$row['District'] ."</td> <td>".$row['Population'] ."</td> </tr>
                ";
                echo "</table><br>";
    }
}else{
    echo "No results";
}

$conn->close();
?>

This is how it looks like in the browser: http://prntscr.com/hty7m9 as you can see, only a one result is inside the table, I would like to be able to show every single result in the table.

Thanks.

Recommended Answers

All 3 Replies

Nope, this didn't change anything, I did finally copy this exemple and I managed to make it work.

Hi ibrsbk

Can you replace the code and try. it will work

<?php
$DBServer = 'localhost';
$DBUser = 'root';
$DBPass = '';
$DBName = 'world';
$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

$sql = "SELECT Id, Name, CountryCode, District, Population FROM city WHERE CountryCode = 'USA'";
$result = $conn->query($sql);
 echo "<table border='1'> <tr> <th>Id</th> <th>Name</th> <th>Code</th> <th>District</th>
if($result->num_rows > 0){
    <th>Population</th> </tr>";
            while($row = mysqli_fetch_array($result)){
                echo"
                <tr> <td>".$row['Id'] ."</td> <td>".$row['Name'] ."</td> <td>".$row['CountryCode'] ."</td> <td>".$row['District'] ."</td> <td>".$row['Population'] ."</td> </tr>
                ";

    }
}else{
    echo "No results";
}
 echo "</table><br>";
$conn->close();
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.