Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime's NOW() function does not work with PDO-MYSQL prepared statements?

I have a database table timetable with a DATETIME field called created_on.

created_on DATETIME,
notes VARCHAR(255) NOT NULL

I use the NOW() function to insert the current time through PDO prepared statements.

INSERT INTO timetable(created_on, note)
VALUES(?, ?);

So after binding the values and executing the prepared statement...

$notes ="This is a note...";
$values =array("NOW()", $notes);
$stm->execute($values);

... the notes column in the MySQL table writes the expected data but the created_on column produces...

0000-00-00 00:00:00 

I don't want to use the Unix epoch timestamp. So how do I get DATETIME in the NOW() function to work?

like image 914
okey_on Avatar asked Dec 31 '12 00:12

okey_on


2 Answers

don't pass NOW() as a parameter,

INSERT INTO timetable(created_on, note) VALUES(NOW(), ?);

so you will only need to bind the notes.

$values =array($notes);
like image 98
John Woo Avatar answered Oct 06 '22 14:10

John Woo


You are passing NOW() as string, not mysql expression.

use as this:

INSERT INTO timetable(created_on, note)
VALUES(now(), ?);
like image 20
t_motooka Avatar answered Oct 06 '22 14:10

t_motooka