开发者

How can I pass MySQL functions as bind parameters in prepared statement?

I'm trying to do this:

$sth = $dbi->prepare('INSERT INTO table VALUES (?, ?, ?)');
$sth->execute(开发者_StackOverflow
    $var1,
    $var2 || 'NOW()',
    $var3
);

without any luck. Any ideas?


$sth = $dbi->prepare('INSERT INTO table VALUES (?, COALESCE(?, NOW()), ?)');
$sth->execute(
    $var1,
    $var2,
    $var3
);


Functions cannot be bound parameters. MySQL will enclose them in quotes which is not valid syntax.

Your options are:

  • DEFAULT CURRENT_TIMESTAMP - If the field is a TIMESTAMP field you can declare it to have a default of the current time like this. This does not work for DATETIME fields.
  • Use perl - $now = time2str('%Y-%m-%d %T', time);


You can use the following coding also.

$sth = $dbi->prepare('INSERT INTO table VALUES (?, COALESCE(?, NOW()), ?)');
$sth->bind_param($var1,$var2,$var3); 
$sth1=$sth->execute;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜