简体   繁体   English

我在此查询中做错了什么?

[英]What am I doing wrong in this query?

There are three tables, 一共有三张桌子

  • storing user details 存储用户详细信息
  • storing groups details 存储组详细信息
  • storing user and group ids. 存储用户和组ID。

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;
}

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 删除了与用户表的不必要的联接,因为如果您有user_id和group_id,则不需要与用户表联接

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 = ?

Your specific issue Error statement is: 您的特定问题错误声明是:

Call to a member function bind_param() on boolean in 在布尔值中调用成员函数bind_param()

at: 在:

 $stmt->bind_param("ii", $user_id, $group_id); 

What this means is that there is no function bind_param() on boolean (true or false) , so this means your $stmt is a boolean, meaning the statement defining line has returned FALSE . 这意味着在布尔值(true或false bind_param()上没有函数bind_param() ,因此这意味着您的$stmt是布尔值,这意味着定义行的语句已返回FALSE

so: 所以:

 $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 = ?"); 

This is where the problem is. 这就是问题所在。 Others in comments have stated that your SQL query is incorrect, which would result in a boolean fail, however, if it is not your SQL query itself that is failing, then you would need to establish that the $this->conn value has been successfully generated and that it is a valid object entity. 其他评论中则指出您的SQL查询不正确,这将导致布尔失败,但是, 如果不是SQL查询本身失败,则需要确定$this->conn值已成功生成,并且它是有效的对象实体。

Try to output an error log something like: 尝试输出类似以下内容的错误日志:

 if(!$stmt = $this->conn->prepare($sql)){
            $errorDump = '
            Error 1 '.date("r").' :
            ';
            $errorDump .= $this->conn->error;
            $errorDump .= "\n\nBacktrace:\n".print_r(debug_backtrace(),TRUE);
            $errorDump .= "
             SQL: ".$sql;
            error_log($errorDump);
            unset($errorDump);
            return false;
        }
    ....
   //carry on with the query as it's ok 

The above is a bit quick and dirty but when your $stmt returns false this error report will tell you why. 上面的代码有点麻烦,但是当您的$stmt返回false时,此错误报告将告诉您原因。 You can then use that error information to solve your Query or your PHP variable structure. 然后您可以使用错误信息来解决您的查询或PHP变量结构。

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

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