Search code examples
phpsqldatabasefetch

Why my php code is not fetching multiple rows from database


Here's my php code unable to fetch multiple rows at a time... as I have seen on internet the functions are right according to the version of php.

<?php 
                        require'db_conn.php';
                        $q1 = "select * from contact";
                        $res = mysqli_query($conn,$q1);
                    echo "
                    <table class='table'>
                        <thead class='thead-dark'>
                            <tr>
                                <th scope='col'>#</th>
                                <th scope='col'>id</th>
                                <th scope='col'>Address</th>
                                <th scope='col'>City</th>
                                <th scope='col'>Country</th>
                                <th scope='col'>State</th>
                                <th scope='col'>Zip</th>
                                <th scope='col'>Phone</th>
                                <th scope='col'>#</th>
                            </tr>
                        </thead>";

                        while ($row = mysqli_fetch_assoc($res)) 
                        {
                            $id = $row["id"];
                            $address = $row["address"];
                            $city = $row["city"];
                            $country = $row["country"];
                            $state = $row["state"];
                            $zip = $row["zip"];
                            $phone = $row["phone"];
                        }

                        echo "
                        <tbody>
                            <tr>
                                <td> <input type='checkbox' name=''> </td>
                                <td>$id</td>
                                <td>$address</td>
                                <td>$city</td>
                                <td>$country</td>
                                <td>$state</td>
                                <td>$zip</td>
                                <td>$phone</td>
                            </tr>
                        </tbody>
                    </table>";
?>

Here's the image contains multiple rows however this code is just fetching single row

enter image description here


Solution

  • The rows are being returned, you just aren't displaying them all because of a logic error - your echo is after the end of your while loop, so it only has access to the last set of data created by the loop, and it only runs once. All the other variables were created, but then overwritten on the next time the loop ran, without being used for anything.

    Simple move the bit which echoes the <tr> within the loop so that it will run repeatedly - once for every row. Obviously you need to keep the bit which echoes the table's end tags separate, after the loop finishes, and put the start of the tbody before the loop.

                        echo "<tbody>";
    
                        while ($row = mysqli_fetch_assoc($res)) 
                        {
                            $id = $row["id"];
                            $address = $row["address"];
                            $city = $row["city"];
                            $country = $row["country"];
                            $state = $row["state"];
                            $zip = $row["zip"];
                            $phone = $row["phone"];
    
                            echo "
                                <tr>
                                    <td> <input type='checkbox' name=''> </td>
                                    <td>$id</td>
                                    <td>$address</td>
                                    <td>$city</td>
                                    <td>$country</td>
                                    <td>$state</td>
                                    <td>$zip</td>
                                    <td>$phone</td>
                                </tr>";
                        }
    
                        echo "</tbody>
                          </table>";