我正在尝试遍历数据库中的表并显示表中的所有详细信息。首先,它应该遍历我的主表'TBook',并获得'date','period',roomID'和'teacherinitials'。然后,使用roomID,它应该在我的另一个表'Rooms'中查找,以获得'room'名称和'description'。之后,它应显示'日期','期间','房间'和& 'description'和'teacherinitials'。
这是我的代码:
<?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>";
?>
但是,我在第56行的/home/user/public_html/my_bookings-results.php中的一个非对象上收到错误说“致命错误:调用成员函数fetch_assoc()”。第56行是这一行:
while ($row2 = $res->fetch_assoc()) {
它确实显示了下面的表格标题,但没有其他内容。出了什么问题?
答案 0 :(得分:0)
首先将循环更改为
while ($row = mysql_fetch_array($result))
在while循环后删除下面的行
$row = mysql_fetch_array($result);
变化:
while ($row2 = $res->fetch_assoc()) {
到
while ($row2 = $res->fetch_array($res)) {