在PHP PDO中使用MySQL函数编写语句

时间:2011-11-28 18:52:38

标签: php mysql pdo

使用PHP PDO时使用MySQL函数的正确方法是什么?函数NOW()被保存为字符串而不是显示时间。

$sth = $dbh->prepare("INSERT INTO pdo (namespace, count, teststring) VALUES (?, ?, ?)");
// these protect you from injection
$sth->bindParam(1, $_a);
$sth->bindParam(2, $_b);
$sth->bindParam(3, $_c);

$_a = 'Wishy-washy';
$_b = 123;
$_c = 'NOW()'; // Doesn't work. Comes out as the string 'NOW()' (w/o the quotes) and not as a date

2 个答案:

答案 0 :(得分:3)

我不会将函数作为绑定参数传递:

$sth = $dbh->prepare("INSERT INTO pdo (namespace, count, teststring) VALUES (?, ?, NOW())");

$_a = 'Wishy-washy';
$_b = 123;

$sth->execute(array($_a, $_b));

答案 1 :(得分:0)

为什么不把它换成像......

$_c = date("H:i:s");

使用PHP日期函数的强大功能?