enchance
enchance

Reputation: 30421

Using MySQL functions in PHP PDO prepared statements

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

Upvotes: 0

Views: 1312

Answers (2)

MrJ
MrJ

Reputation: 1938

Why not replace it to something like..

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

Using the power of the PHP date function?

Upvotes: 0

Xeoncross
Xeoncross

Reputation: 57184

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));

Upvotes: 3

Related Questions