dtstnjl898781429
2016-06-07 03:08
浏览 75
已采纳

如何在mysqli预处理语句中使用多个内连接以及多个WHERE子句? [重复]

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();
</div>

图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:

  • 致命错误:在布尔值上调用成员函数bind_param() 11 answers
  • 如何在不同环境中获取MySQLi错误信息? / mysqli_fetch_assoc()要求参数1为mysqli_result 1回答

    我正在尝试编写一个查询,我想根据第一个连接条件和WHERE子句选择 第二个WHERE子句基于第二个连接。 我已经写了几个方法,但它总是显示错误:

     致命错误:在/ var / www /中调用boolean上的成员函数bind_param() 第80行上的html / connections.php 
       
     
     

    $ user_id正在从其他地方获取。 所以没有问题,如果我在这里运行一个连接,它工作正常。

    这是我的查询:

    方法1:

      $ sql = $ db-&gt; prepare(“在r.id = c2.uid 
    内部联接连接c中,来自registered_users r 
    内部联接连接c2的SELECT r。* 在r.id = c.connections_userid,其中c2.connections_userid =?或c.uid =?“); 
     
     $ sql-&gt; bind_param(”ii“,$ user_id,$ user_id);  
     $ sql-&gt; execute(); 
       
     
     

    方法2:

      $ sql = $ db  - &gt; prepare(“来自于r.id = c2.uid的registered_users r 
    内部联接连接c2的SELECT r。*,其中c2.connections_userid =?
     r.id = c.connections_userid处的内部联接连接c,其中c。  uid =?“); 
     
     $ sql-&gt; bind_param(”ii“,$ user_id,$ user_id);  
     $ sql-&gt; execute(); 
       
     
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsz1966 2016-06-07 03:30
    已采纳

    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();
    
    打赏 评论

相关推荐 更多相似问题