0

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.

Edited by ibrsbk

3
Contributors
3
Replies
31
Views
6 Months
Discussion Span
Last Post by rpv_sen
0

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

0

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();
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.