2012-08-05 139 views
1

以下錯誤會觸發mysql錯誤,這是由於查詢沒有將該參數綁定到語句。我不明白爲什麼會發生這種情況。mysqli bind_param失敗,但爲什麼?

這是else子句返回的錯誤:

ERROR -> 1064 : 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 '? ORDER BY dateCreated DESC' at line 4` 

這裏是有問題的代碼:

$userId = 1; 
if ($stmt = $link->query(" 
    SELECT o.id, dateCreated, firstValue 
    FROM user_orders o 
    LEFT JOIN order_delivery d ON o.id = d.id 
    WHERE o.userId = ? 
    ORDER BY dateCreated DESC 
")) 
{ 
    $stmt->bind_param("i", $userId); 
    $stmt->execute(); 
    $stmt->close(); 
} 
else 
{ 
    $pageContent = ' 
     <p>ERROR -> '.$link->errno.' : '.$link->error.'</p> 
    '; 

} 

可能有人可能指向我在哪裏出了毛病這一點,爲什麼這個發生mysql錯誤。

感謝您花時間閱讀本文!

回答

4

使用mysqli::prepare而不是query來準備您的預備聲明。

query將嘗試運行該查詢,該參數在綁定參數前無效。