Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to loop through a table in my database and show all the details in a table. Firstly, it should loop through my main table, 'TBook', and get the 'date', 'period', roomID', and 'teacherinitials'. Then, using the roomID, it should look in my other table, 'Rooms', to get the 'room' name and the 'description'. After that it should display the 'date', 'period', 'room' & 'description', and 'teacherinitials'.

This is my code:

<?php
            // Create connection
            $con=mysqli_connect("host","user","pass","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);

            $sql2="SELECT room, description FROM Rooms WHERE roomID = $roomID";
            $res=mysqli_query($con,$sql2);

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

            // Loop through database
            while ($row = $result->fetch_assoc()) {
                $row = mysql_fetch_array($result);
                $date = $row['date'];
                $period = $row['period'];
                $roomID = $row['roomID'];
                $teacherinitials = $row['teacherinitials'];

                    while ($row2 = $res->fetch_assoc()) {               
                            $room = $row2['room'];
                            $description = $row2['description'];
                    }

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

            }

            echo "</table>";
    ?>

However, instead I get an error saying "Fatal error: Call to a member function fetch_assoc() on a non-object in /home/user/public_html/my_bookings-results.php on line 56". Line 56 is this line:

while ($row2 = $res->fetch_assoc()) {

It does show the table headers beneath that, but nothing else. What is going wrong?

share|improve this question
    
what is $row2?? –  Leonardo Jan 17 at 13:13
    
It's a variable to hold the row. I was told that is how you loop through. I'm a little lost to be honest. Not too experienced with PHP and SQL. –  user2397282 Jan 17 at 13:14

1 Answer 1

change first while loop to

 while ($row = mysql_fetch_array($result))

remove below line after while loop

$row = mysql_fetch_array($result);

change:

while ($row2 = $res->fetch_assoc()) {

to

while ($row2 = $res->fetch_array($res)) {
share|improve this answer
    
Thanks, this got rid of the error, but there is stil nothing in my tables? –  user2397282 Jan 17 at 13:16
    
i just edited the answer.... –  ReNiSh A R Jan 17 at 13:18
    
if you found its working mark it as accepted.... –  ReNiSh A R Jan 17 at 13:19
    
It's still not showing anything in my tables; not sure why? –  user2397282 Jan 17 at 13:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.