0

I am trying to loop through my database and display all the entries.

Here is my code:

<?php
        // Create connection
        $con=mysqli_connect("host","username","password","database");

        // Check connection
        if (mysqli_connect_errno($con)) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        //Get number of rows
        $sql="SELECT * FROM TBook";
        $result=mysqli_query($con, $sql);
        $rowcount=mysqli_num_rows($result);

        //Start table
        echo "<table>";
        echo "<tr><th>Date</th><th>Period</th><th>Room</th><th>Teacher Initials</th></tr>";

        // Loop through database
        for ($i = 1; $i < $rowcount; $i++) {
            $row = mysql_fetch_array($result);
            $date = $row['date'];
            $period = $row['period'];
            $room = $row['room'];
            $teacherinitials = $row['teacherinitials'];

        // Show entries
            echo    "<tr>
                <td>".$date."</td>
                <td>".$period."</td>
                <td>".$room."</td>
                <td>".$teacherinitials."</td>
                </tr>";

        }

        echo "</table>"
?> 

When I run it, however, the headings of the columns show but nothing else.

What is going wrong?

2
  • 1
    If only 1 row is returned that it will never display for ($i = 0; $i < $rowcount; $i++) ... but you're combining MySQLi and MySQL: instead of for ($i = 1; $i < $rowcount; $i++) { $row = mysql_fetch_array($result);, use while ($row = $result->fetch_assoc()) { Commented Jan 16, 2014 at 19:12
  • what you want is: while ($row = mysql_fetch_array($result)) { .... } Commented Jan 16, 2014 at 19:13

1 Answer 1

2

Maybe you can try this code:

     // Check connection
    if (mysqli_connect_errno($con)) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    //Get number of rows
    $sql="SELECT * FROM TBook";
    $result=mysqli_query($con, $sql);
    $i=1;
    while($row=mysqli_fetch_assoc($result))
    {
        $date[$i] = $row['date'];
        $period[$i] = $row['period'];
        $room[$i] = $row['room'];
        $teacherinitials[$i] = $row['teacherinitials'];
        $i++;
    }  
    //Start table
    echo "<table>";
    echo "<tr><th>Date</th><th>Period</th><th>Room</th><th>Teacher Initials</th></tr>";

    // Loop through the results from the database
    for ($i = 1; $i <=count($date); $i++) 
    {
    // Show entries
        echo    
            "<tr>
            <td>$date[$i]</td>
            <td>$period[$i]</td>
            <td>$room[$i]</td>
            <td>$teacherinitials[$i]</td>
            </tr>";

    }

    echo "</table>"
 ?> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.