[英]Escaping a colon (:) in a PDO prepared statement
$sql3 = 'SELECT sched_id, date_format(sched_date_time,\'%H:%i\') AS \'Time\'
FROM schedule
WHERE (date_format(sched_date_time,\'%Y-%m-%d\') = \':date\') AND schedule.film_id = :film_id';
$sth2 = $pdo->prepare($sql3);
$sth2->bindValue(':date','2021-12-18');
// date_format($date,"%Y-%m-%d")
$sth2->bindValue(':film_id',$row1['film_id']);
$sth2->execute();
I am getting the following error我收到以下错误
"Invalid parameter number: number of bound variables does not match number of tokens"
“无效的参数号:绑定变量的数量与标记的数量不匹配”
I belive this is being caused by the colon in the variable $sql3
in the SQL Function date_format()
.我相信这是由 SQL Function
date_format()
中的变量$sql3
中的冒号引起的。
How do I escape the colon so that I can keep it for formatting without the PDO thinking its a declaration for a placeholder?如何转义冒号,以便在没有 PDO 认为它是占位符声明的情况下保留它以进行格式化?
I have to use \\
before the colon to escape it as per answers in other questions but I continue to get the error.根据其他问题的答案,我必须在冒号之前使用
\\
来转义它,但我继续收到错误消息。
I am using XAMPP as a portable/temporary development enviroment.我正在使用 XAMPP 作为便携式/临时开发环境。 XAMPP uses MariaDB as its database.
XAMPP 使用 MariaDB 作为其数据库。
To prevent the need to escape quotes, define the SQL statement within double quotes:为避免需要转义引号,请在双引号内定义 SQL 语句:
$sql3 = "SELECT sched_id, date_format(sched_date_time,'%H:%i') AS 'Time'
FROM schedule
WHERE (date_format(sched_date_time,'%Y-%m-%d') = :date)
AND schedule.film_id = :film_id";
$sth2 = $pdo->prepare($sql3);
$sth2->bindValue(':date','2021-12-18', PDO::PARAM_STR);
// date_format($date,"%Y-%m-%d")
$sth2->bindValue(':film_id',$row1['film_id'], PDO::PARAM_INT);
$sth2->execute();
As per the bindValue documentation , you can also explicitly set the type of the value.根据bindValue 文档,您还可以显式设置值的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.