mysqli bind_param失败了,但原因是什么


mysqli bind_param fails but why?

下面的错误触发了一个mysql错误,这是由于查询没有将参数绑定到语句。我不明白为什么会发生这种事。

这是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错误。

谢谢你花时间通读这篇文章!

使用mysqli::prepare而不是query来准备准备准备好的语句。

query将尝试运行在绑定参数之前无效的查询。