douchan7552 2016-06-28 19:42
浏览 39

我在这个查询中做错了什么?

There are three tables,

  • storing user details
  • storing groups details
  • storing user and group ids.

I need to check if a user is already a member of one group. I'm using this query to achieve that:

SELECT u.id, g.id 
FROM users u, groups g 
INNER JOIN user_groups ug 
ON ug.user_id = u.id AND ug.group_id = g.id 
WHERE ug.user_id = ? AND ug.group_id = ?

but this is throwing me an error:

Call to a member function bind_param() on boolean in

I have checked if i have misspelled some word in my query and everything is okay.

EDIT:

Here is a function:

public function isUserMember($user_id, $group_id) {
        $stmt = $this->conn->prepare("
            SELECT u.id, g.id from users u, groups g 
            INNER JOIN user_groups ug 
            ON ug.user_id = u.id AND ug.group_id = g.id 
            WHERE ug.user_id = ? AND ug.group_id = ?");
        $stmt->bind_param("ii", $user_id, $group_id); // here i'm getting an error
        $stmt->execute();
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();
        return $num_rows > 0;
}
  • 写回答

2条回答 默认 最新

  • dongshaidu2456 2016-06-28 20:01
    关注

    Try following changes

    removed unnecessary join with user table because if you have user_id and group_id you don't need to join it with user

    SELECT ug.id, g.id 
    from  user_groups ug  
    inner join  groups g 
    on ug.group_id = g.id 
    WHERE ug.user_id = ? AND ug.group_id = ?
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序