Using MySQL functions in PHP PDO prepared statements

你。 提交于 2019-12-25 03:59:19

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!