仅应通过引用传递变量 - 当将 LIMIT 与 bindParam 一起使用时

Only variables should be passed by reference - when using LIMIT with bindParam

我想将 LIMITbindParam 一起使用,所以我构建了这个查询:

$app->get('/contact/get_contacts/{contact_number}', function (Request $request, Response $response, array $args)
{
    $query = "SELECT * FROM contact LIMIT :contact_number";
    $sql->bindParam("contact_number", intval($args["contact_number"]), PDO::PARAM_INT);
    $sql->execute();
    $result = $sql->fetchAll();
    return $response->withJson($result);
});

我收到这条通知:

Only variables should be passed by reference

我做错了什么?我正在使用 Slim FrameworkPDO

您需要更改:

$sql->bindParam("contact_number", intval($args["contact_number"]), PDO::PARAM_INT);

$sql->bindParam(":contact_number", $args["contact_number"], PDO::PARAM_INT);
  • 我在参数名称前添加了缺少的 :
  • 我已经删除了 intval 函数调用,如果您仍想使用该函数,请在 bindParam 函数调用之外使用它。

根据函数定义:

public bool PDOStatement::bindParam ( mixed $parameter , mixed &$variable [, int $data_type = PDO::PARAM_STR [, int $length [, mixed $driver_options ]]] )

绑定参数是一个variable by reference (&$variable).

阅读Material

bindParam

您在处理数据类型时是双重的。明确指定 PDO::PARAM_INT 时不需要 intval。只需删除 intval 或查看 bindValue:

的原因
$sql->bindValue(":contact_number", intval($args["contact_number"]));