【发布时间】:2018-03-28 16:25:16
【问题描述】:
当一个 PDO for MySQL 准备好的语句有:
- 参数(
v = '') - 名称包含单引号 (
f'f) 的字段 -
VALUES部分带有参数,后跟一个值 单引号 ('')
然后它失败并出现异常:
SQLSTATE[42000]: Syntax error or access violation: 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 ':v,'')' at line 1
一些代码重现:
function query($sql) {
$pdo = new PDO('mysql:host=...', 'u', 'p', [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ]);
$pdo->prepare($sql)->execute(['v' => '']);
}
query("INSERT INTO `t` (`f'f`,`f`) VALUES ('',:v)"); // ok
query("INSERT INTO `t` (`f`,`f'f`) VALUES ('',:v)"); // ok
query("INSERT INTO `t` (`f'f`,`f`) VALUES (:v,'')"); // exception
query("INSERT INTO `t` (`f`,`f'f`) VALUES (:v,'')"); // exception
query("INSERT INTO `t` (`f'f`,`f`) VALUES (null,:v)"); // ok
query("INSERT INTO `t` (`f`,`f'f`) VALUES (null,:v)"); // ok
query("INSERT INTO `t` (`f'f`,`f`) VALUES (:v,null)"); // ok
query("INSERT INTO `t` (`f`,`f'f`) VALUES (:v,null)"); // ok
PHP 版本 5.5.9-1ubuntu4.24,mysqlnd 5.0.11
这是一个错误还是我遗漏了什么?
更新:
在 PHP 版本 7.1.15-1、mysqlnd 5.0.12 上试过:没有变化。
尝试使用位置参数:没有变化。
【问题讨论】:
标签: php pdo prepared-statement