简体   繁体   English

MySQL 错误:查询未提供正确数据

[英]MySQL Error: query not giving correct data

I do a mySQL query to get some data and then (for the purpose of debugging) print it out.我执行 mySQL 查询以获取一些数据,然后(出于调试目的)将其打印出来。 In this particular sample there are 5 rows of data and each room_id in the database table has a value.在此特定示例中,有 5 行数据,数据库表中的每个 room_id 都有一个值。 However the print-out only shows the room_id of the first row.然而,打印输出只显示第一行的 room_id。

$query_rooms = "SELECT room_id FROM lh_rooms WHERE hid = '$hid'";
$rooms = mysql_query($query_rooms, $MySQL) or die(mysql_error());
$row_rooms = mysql_fetch_assoc($rooms);
$numrows = mysql_num_rows($rooms);
    $i = 0;
while ($i < $numrows) {
    $room_id=$row_rooms['room_id'][$i];
echo $i." - ".$room_id."<br><br>";
   ++$i;
}

0 - 2 0 - 2

1 - 1 -

2 - 2 -

3 - 3 -

4 - 4 -

Can someone explain what is happening有人可以解释发生了什么

You are fetching multiple rows.您正在获取多行。

So, you need to loop over the result set instead of fetching just one time.因此,您需要遍历结果集而不是只获取一次。

Corrected code:更正的代码:

$query_rooms = "SELECT room_id FROM lh_rooms WHERE hid = '$hid'";
$rooms = mysql_query($query_rooms, $MySQL) or die(mysql_error());
$i=0;
while($row_rooms =  mysql_fetch_assoc($rooms)) {
    $room_id=$row_rooms['room_id'];
    echo $i." - ".$room_id."<br><br>";
    ++$i;
}

Note: Never use mysql_注意:永远不要使用mysql_ , they are deprecated and will be removed in the upcoming versions. ,它们已被弃用,并将在即将推出的版本中删除。 Use mysqli_ or PDO instead.请改用mysqli_PDO

Try like this像这样尝试

$query_rooms = "SELECT room_id FROM lh_rooms WHERE hid = '$hid'";
$rooms = mysql_query($query_rooms, $MySQL) or die(mysql_error());
 $i = 0;
while ($row_rooms = mysql_fetch_assoc($rooms)) {
$room_id=$row_rooms['room_id'];
 echo $i." - ".$room_id."<br><br>";
$i++;
}

You are looping $i instead of looping the $row_rooms.您正在循环 $i 而不是循环 $row_rooms。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM