PDO - 在 mysql 上尝试绑定 BETWEEN 中的值时出错
PDO - Getting error while trying to bind values in BETWEEN on mysql
我正在尝试使用 PDO 和 MySql 创建一个 select 语句。我有以下代码:
$this->bind[':created'] = "'" . (new DateTime('First day of this month'))->format('Y-m-d') . "' AND '" . date('Y-m-d') . "'";
$this->where[] = "created BETWEEM :created";
如您所见,我在 MySql documentation 之后用引号括起日期,但 PHP 正在转义它,我收到了一个错误。
'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 'created BETWEEN '\'2015-07-01\' AND \'2015-07-24\'' '
那么,我如何使用 PDO::bindValue() 的范围日期?
更具体地说,我想得到这个结果:
SELECT * FROM table WHERE created BETWEEN '2015-07-01' AND '2015-07-24'
而不是:
SELECT * FROM table WHERE created BETWEEN '\'2015-07-01\' AND '15-07-24\''
谢谢。
绑定适用于单个值。您正在尝试绑定多个值。您需要:
... WHERE foo BETWEEN :start AND :end
和两个绑定调用。
记住,占位符基本上有点像变量,除了数据库知道这个 "variable" 只能表示一个值。它会 NOT 挑选你传入的值并尝试使其符合语句,所以
foo BETWEEN :thingie
与
没有区别
foo BETWEEEN '$var'
就插入值的数量而言。
我正在尝试使用 PDO 和 MySql 创建一个 select 语句。我有以下代码:
$this->bind[':created'] = "'" . (new DateTime('First day of this month'))->format('Y-m-d') . "' AND '" . date('Y-m-d') . "'";
$this->where[] = "created BETWEEM :created";
如您所见,我在 MySql documentation 之后用引号括起日期,但 PHP 正在转义它,我收到了一个错误。
'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 'created BETWEEN '\'2015-07-01\' AND \'2015-07-24\'' '
那么,我如何使用 PDO::bindValue() 的范围日期?
更具体地说,我想得到这个结果:
SELECT * FROM table WHERE created BETWEEN '2015-07-01' AND '2015-07-24'
而不是:
SELECT * FROM table WHERE created BETWEEN '\'2015-07-01\' AND '15-07-24\''
谢谢。
绑定适用于单个值。您正在尝试绑定多个值。您需要:
... WHERE foo BETWEEN :start AND :end
和两个绑定调用。
记住,占位符基本上有点像变量,除了数据库知道这个 "variable" 只能表示一个值。它会 NOT 挑选你传入的值并尝试使其符合语句,所以
foo BETWEEN :thingie
与
没有区别foo BETWEEEN '$var'
就插入值的数量而言。