问题
What's the right way of using a MySQL function while using PHP PDO? The function NOW() gets saved as a string instead of showing the time.
$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
回答1:
I would not pass functions as the bound params:
$sth = $dbh->prepare("INSERT INTO pdo (namespace, count, teststring) VALUES (?, ?, NOW())");
$_a = 'Wishy-washy';
$_b = 123;
$sth->execute(array($_a, $_b));
回答2:
Why not replace it to something like..
$_c = date("H:i:s");
Using the power of the PHP date function?
来源:https://stackoverflow.com/questions/8300766/using-mysql-functions-in-php-pdo-prepared-statements