简体   繁体   中英

What am I doing wrong in this query?

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

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

Your specific issue Error statement is:

Call to a member function bind_param() on boolean in

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 .

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.

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. You can then use that error information to solve your Query or your PHP variable structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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