num_rows不能使用“SELECT * FROM table_name”

时间:2017-01-11 23:17:16

标签: php mysql

过去几个小时为什么mysqli_num_rows无法使用sql-query "SELECT * FROM table_name"让我感到惊讶,我只是不断收到错误

  

警告:mysqli_num_rows()期望参数1为mysqli_result,boolean given

我的实际代码是(实际上它有点简化):

  $checkLists = "SELECT * FROM todo_list WHERE group_id=(SELECT group_id FROM user_group WHERE user_id='$user_id')";
  $resultCheck = mysqli_query($conn, $checkLists);
  $numberList = mysqli_num_rows($resultCheck);

如何从此查询中获取行数?我希望有人可以帮助我,提前谢谢!

1 个答案:

答案 0 :(得分:2)

查询错误。当子查询最多返回1行时,您只能使用WHERE column = (subquery)。如果子查询返回多于1行,则无法将其与=进行比较,并且您将收到错误消息。测试列是否与返回的任何结果匹配的正确方法是IN,而不是=

但通常最好使用JOIN代替WHERE IN

SELECT DISTINCT t.*
FROM todo_list AS t
JOIN user_group AS u ON u.group_id = t.group_id
WHERE u.user_id = '$user_id'