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?