2012-08-05 145 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将尝试运行该查询,该参数在绑定参数前无效。