2016-06-07 19 views
0

我想寫一個查詢,我想根據第一個連接條件選擇WHERE子句,第二個連接是基於第二個WHERE子句。我已經寫了幾種方法,但它總是顯示一個錯誤:如何在mysqli準備的stanment中使用多個內部連接以及多個WHERE子句?

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

$ user_id正在從其他地方獲取。所以沒有問題,如果我在這裏運行單個連接,它工作正常。

這裏是我的查詢:

方法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(); 

方法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(); 
+0

我已經在這裏嘗試了一切,但事實並非如此似乎爲我工作。 –

回答

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

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

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

注意:您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以在'r.id = c.connections_userid'處使用'內部連接連接c'附近使用的正確語法,其中c.uid =?'在第3行/var/www/html/connections.php在線81 –

+0

你走了,現在不是更有幫助嗎?如果你仍然無法弄清楚錯誤發生了什麼問題,那麼發佈一個新的問題,包括你的表格模式。 –

+1

我知道我需要在這個語句之後使用錯誤日誌記錄,但是我正在爲準備好的語句的語法錯誤而煩惱,就像在這個trigger_error($ db-> error)中一樣。這真的很有幫助。它也給了我一個使用UNION運算符的想法,而不是用這種愚蠢的方式寫它。再次感謝:) –

相關問題