Fatal error: Uncaught Error: Cannot pass parameter 2 by reference PDO
Fatal error: Uncaught Error: Cannot pass parameter 2 by reference PDO
我正在尝试在 PDO 中使用 LIKE 进行查询,但是我收到以下错误:致命错误:未捕获错误:无法通过引用 PDO 传递参数 2,在这部分代码中。我该如何解决?
function buscador($DB,$categoria_id,$buscado){
$productos = NULL;
$sql = "SELECT * from producto WHERE categoria_id = ? LIKE ?";
$stmt = $DB->prepare($sql);
//$params = [$categoria_id,':keywords', '%' . $buscado . '%'];
$stmt->bindParam(1,$categoria_id,PDO::PARAM_STR);
$stmt->bindParam(2,':keywords', '%' . $buscado . '%',PDO::PARAM_STR);
$stmt->execute();
$productos = $stmt->fetchAll();
return $productos;
}
bindParam
的第二个参数是通过引用传递的,因此不能是文字,它必须是变量。
我修复的查询语法中也有错误,虽然我不知道要使用的列名,请在尝试使用此代码之前更改它。
function buscador($DB,$categoria_id,$buscado){
$productos = NULL;
$sql = "SELECT * from producto WHERE categoria_id = ? AND othercol LIKE ?";
$stmt = $DB->prepare($sql);
$p2 = '%' . $buscado . '%';
$stmt->bindParam(1,$categoria_id,PDO::PARAM_STR);
$stmt->bindParam(2,$p2,PDO::PARAM_STR);
$stmt->execute();
$productos = $stmt->fetchAll();
return $productos;
}
我正在尝试在 PDO 中使用 LIKE 进行查询,但是我收到以下错误:致命错误:未捕获错误:无法通过引用 PDO 传递参数 2,在这部分代码中。我该如何解决?
function buscador($DB,$categoria_id,$buscado){
$productos = NULL;
$sql = "SELECT * from producto WHERE categoria_id = ? LIKE ?";
$stmt = $DB->prepare($sql);
//$params = [$categoria_id,':keywords', '%' . $buscado . '%'];
$stmt->bindParam(1,$categoria_id,PDO::PARAM_STR);
$stmt->bindParam(2,':keywords', '%' . $buscado . '%',PDO::PARAM_STR);
$stmt->execute();
$productos = $stmt->fetchAll();
return $productos;
}
bindParam
的第二个参数是通过引用传递的,因此不能是文字,它必须是变量。
我修复的查询语法中也有错误,虽然我不知道要使用的列名,请在尝试使用此代码之前更改它。
function buscador($DB,$categoria_id,$buscado){
$productos = NULL;
$sql = "SELECT * from producto WHERE categoria_id = ? AND othercol LIKE ?";
$stmt = $DB->prepare($sql);
$p2 = '%' . $buscado . '%';
$stmt->bindParam(1,$categoria_id,PDO::PARAM_STR);
$stmt->bindParam(2,$p2,PDO::PARAM_STR);
$stmt->execute();
$productos = $stmt->fetchAll();
return $productos;
}