faisal abdulai
faisal abdulai

Reputation: 3819

Populating HTML Table with Data from mysql Gives Empty Table Cells

I want to populate a table with data from a MySQL database with PHP, but the table cells remain empty when the code is executed, and I don't receive any errors

Below is the code:

<?php
$host = "localhost"; // Host name 
$username = ""; // Mysql username 
$password = ""; // Mysql password 
$db_name = "test"; // Database name 
$tbl_name = "test_mysql"; // Table name 
$server_name = "localhost";

// Create connection
$con = new mysqli($server_name, $username, $password, $db_name, 3306);
if($con->connect_error){
    die("Connection failed: ".$con->connect_error);
}

// Check connection
if($con->connect_error){
    die("Connection failed: ".$conn->connect_error);
}

$sql = "SELECT * FROM $tbl_name";
$result = $con->query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
        <td>
            <table width="400" border="1" cellspacing="0" cellpadding="3">
                <tr>
                    <td colspan="4"><strong>List data from mysql</strong></td>
                </tr>
                <tr>
                    <td align="center"><strong>Name</strong></td>
                    <td align="center"><strong>Lastname</strong></td>
                    <td align="center"><strong>Email</strong></td>
                    <td align="center"><strong>Update</strong></td>
                </tr>
                <?php
                if($result->num_rows > 0){
                    // output data of each row
                    while($row = $result->fetch_assoc()){ ?>
                        <tr>
                            <td><? echo $rows['name']; ?></td>
                            <td><? echo $rows['lastname']; ?></td>
                            <td><? echo $rows['email']; ?></td>
                            <td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>
                        </tr>
                        <?php
                    }
                }
                ?>
            </table>
        </td>
    </tr>
</table>
<?php
$con->close();
?>

I think it could be missing a code, I appreciate any help you can give me!

Upvotes: 0

Views: 711

Answers (5)

My Solution would be for you to add below

$sql = "SELECT * FROM $tbl_name";
$result = $con->query($sql);

var_dump($result);die; //Include this line of code and see if it is actually getting what you selected from the database. If you can get those records inside the var_dump(), then you can tell what next to do

Upvotes: 1

Malik Naik
Malik Naik

Reputation: 1502

Try this...

<?php
$host = "localhost"; // Host name 
$username = ""; // Mysql username 
$password = ""; // Mysql password 
$db_name = "test"; // Database name 
$tbl_name = "test_mysql"; // Table name 
$server_name = "localhost";

// Create connection
$con = new mysqli($server_name, $username, $password, $db_name, 3306);
if($con->connect_error){
   die("Connection failed: ".$con->connect_error);
}

// Check connection
if($con->connect_error){
 die("Connection failed: ".$conn->connect_error);
}

$sql = "SELECT * FROM $tbl_name";
$result = $con->query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
    <td>
        <table width="400" border="1" cellspacing="0" cellpadding="3">
            <tr>
                <td colspan="4"><strong>List data from mysql</strong></td>
            </tr>
            <tr>
                <td align="center"><strong>Name</strong></td>
                <td align="center"><strong>Lastname</strong></td>
                <td align="center"><strong>Email</strong></td>
                <td align="center"><strong>Update</strong></td>
            </tr>
            <?php
            if($result->num_rows > 0){
                // output data of each row
                while($rows = $result->fetch_assoc()){ ?>
                    <tr>
                        <td><?php echo $rows['name']; ?></td>
                        <td><?php echo $rows['lastname']; ?></td>
                        <td><?php echo $rows['email']; ?></td>
                        <td align="center"><a href="update.php?id=<?php echo $rows['id']; ?>">update</a></td>
                    </tr>
                    <?php
                }
            }
            ?>
        </table>
    </td>
</tr>
</table>
<?php
$con->close();
?>

Upvotes: 1

Bhavesh G
Bhavesh G

Reputation: 3028

You have used $row Instead of $rows in while loop declaration.

while($rows = $result->fetch_assoc()){
    echo"<tr>
           <td>{$rows['name']}</td>
           <td>{$rows['lastname']}</td>
           <td>{$rows['email']}</td>
           <td align='center'><a href='update.php?id={$rows['id']}'>update</a></td>
         </tr>"
}

Upvotes: 1

Macerier
Macerier

Reputation: 310

you use $rows instead of $row this is you correct code:

<tr>
    <td><? echo $row['name']; ?></td>
    <td><? echo $row['lastname']; ?></td>
    <td><? echo $row['email']; ?></td>
    <td align="center"><a href="update.php?id=<? echo $row['id']; ?>">update</a></td>
</tr>

Upvotes: 0

varesa
varesa

Reputation: 2419

One issue you have is different variable names

Here is variable $row:

while($row = $result->fetch_assoc()){

Here is variable $rows:

<td><? echo $rows['name']; ?></td>

I would expect that to atleast give an warning somewhere though

Upvotes: 0

Related Questions