1

I am trying to write a query where I want to select with WHERE clause based on the first join condition and the second WHERE clause based on the second join. I have written it a couple of ways, but it always shows up an error:

Fatal error: Call to a member function bind_param() on boolean in /var/www/html/connections.php on line 80

$user_id is being fetched from somewhere else. so no issues with that, If I run a single join here, it works fine.

Here is my query :

Method 1 :

$sql = $db->prepare("SELECT r.* from registered_users r 
        inner join connections c2 on r.id = c2.uid
        inner join connections c on r.id = c.connections_userid where c2.connections_userid = ? or c.uid = ?");

$sql->bind_param("ii",$user_id,$user_id);           
$sql->execute();

Method 2 :

$sql = $db->prepare("SELECT r.* from registered_users r 
        inner join connections c2 on r.id = c2.uid where     c2.connections_userid = ?
        inner join connections c on r.id = c.connections_userid where c.uid = ?");

$sql->bind_param("ii",$user_id,$user_id);           
$sql->execute();

1 Answers1

1

You have an error with your prepare, so check it

$sql = $db->prepare("SELECT r.* from registered_users r 
    inner join connections c2 on r.id = c2.uid
    inner join connections c on r.id = c.connections_userid where c2.connections_userid = ? or c.uid = ?");

if (!$sql) {
   //error !! Let's report it!
   trigger_error($db->error);
}

$sql->bind_param("ii",$user_id,$user_id);           
$sql->execute();
Your Common Sense
  • 154,967
  • 38
  • 205
  • 325
Jeff Puckett
  • 33,491
  • 16
  • 111
  • 160
  • Notice: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'inner join connections c on r.id = c.connections_userid where c.uid = ?' at line 3 in /var/www/html/connections.php on line 81 – Bishwaroop Chakraborty Jun 07 '16 at 14:21
  • There you go, now isn't that more helpful? If you still can't figure out what's wrong given that error, then post a new question including your table schemas. – Jeff Puckett Jun 07 '16 at 14:29
  • 1
    I knew that I need to use error logging after this statement, but I was struggling with the prepared statement syntax for error as in this trigger_error($db->error). That was really helpful. Also it gave me an idea to use a UNION operator instead of writing it this silly way. Thanks again :) – Bishwaroop Chakraborty Jun 07 '16 at 14:43
  • 1
    I upvoted your answer too, but I guess it wont show up right now or something since I joined anew – Bishwaroop Chakraborty Jun 07 '16 at 14:43
  • Glad to hear that! I think you can still accept the answer by clicking the checkmark. – Jeff Puckett Jun 07 '16 at 14:46