为什么不能将 MYSQL 函数传递到准备好的 PDO 语句中?
在我看来,以下脚本应该可以工作:
$stmt = $db->prepare("UPDATE table SET status = ?, date_modified = ?");
$stmt->execute(array(1, 'NOW()'));
但是当将 NOW()
传递到准备好的语句中时,什么也没有发生。将 NOW()
替换为实际日期(即 2010-11-23)就可以了。
我无法在网上找到解释。有什么想法吗?
编辑
只是为了进一步澄清并消除问题中的任何混乱,我想实际上将一个变量传递到准备好的语句中,但是,该变量将被设置为 mysql 的五个可能的日期/时间函数之一。
例如
$var = 'NOW()';
$var = 'LAST_DAY(DATE_ADD(CURDATE(), 间隔 1 个月))';
$var = 'LAST_DAY(CURDATE())';
...等等...
准备好的语句变成:
$stmt->execute(array(1, $var));
我知道这会返回相同的 NULL 结果,但我担心如果我简单地将 sql 语句更改为:
更新表设置状态 = ?, date_modified = $var
我要开放自己进行注入吗?
In my mind, the following script should work:
$stmt = $db->prepare("UPDATE table SET status = ?, date_modified = ?");
$stmt->execute(array(1, 'NOW()'));
but when passing NOW()
into the prepared statement, nothing happens. Replacing NOW()
with an actual date (i.e. 2010-11-23) works just fine.
I am unable to find explanation online. Any ideas?
EDIT
Just to further clarify and rid of any confusion in the question, I want to actually pass a variable into the prepared statement HOWEVER, the variable will be set to one of five possible date/time functions for mysql.
e.g.
$var = 'NOW()';
$var = 'LAST_DAY(DATE_ADD(CURDATE(),
INTERVAL 1 MONTH))';$var = 'LAST_DAY(CURDATE())';
... and so on...
prepared statement turns into:
$stmt->execute(array(1, $var));
I know this will return the same NULL results, but I am worried if I simply change the sql statement to:
UPDATE table SET status = ?,
date_modified = $var
I am opening myself to injection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要将
NOW()
作为参数传递,因为无需对其进行任何处理,因为它是内置的 SQL 函数,因此只需将其包含在实际查询中,如下所示。或者,您可以将
date_modified
设置为 TIMESTAMP 字段,它会自动更新 SQL 更新上的date_modified
字段。You do not need to pass
NOW()
as a parameter as there is no need to do any processing on it, given it is a built in SQL Function, so just include it in the actual query like below.Alternatively, you can just set the
date_modified
to a TIMESTAMP field and it will automatically update thedate_modified
field on a SQL Update.准备好的语句将您插入其中的所有内容解释为文字字符串。这是为了防止任何类型的不可预测的 SQL 注入。
实际发生的情况是
NOW()
尝试在读取时插入数据库(字面意思是NOW()
),而不是获取要插入的实际日期。然后,它可能在数据库中显示为空白,因为您有一个日期列,它不会将NOW()
解释为日期,因此不接受它。如果可能,您应该尝试在不使用任何替换方法的情况下执行 SQL,因为这种方法没有任何危险。
Prepared statements interpret everything you insert into them as a literal string. This is to prevent any type of unpredictable SQL injection.
What is actually happening is that
NOW()
is attempting to be inserted into the database just as it reads (literally,NOW()
) instead of getting the actual date to insert. It is then probably showing blank in your database because you have a date column, which doesn't interpretNOW()
as a date and therefore doesn't accept it.If possible, you should try to execute the SQL without using any substitution methods as there is nothing dangerous to this approach.
我的猜测是,PDO 假设 'NOW()' 是一个字符串,并在填充查询参数时将其括在引号中。我将使用 PHP 函数 date('Ymd') 传递当前日期,这会给您相同的结果。
My guess is that PDO is assuming 'NOW()' is a string and enclosing it in quotes when populating the query parameters. I would just pass the current date using the PHP function date('Y-m-d'), which will give you the same results.