Baloo
Baloo

Reputation: 221

MySQL error with dates

MySQL tells me I have a wrong syntax, but I don't know where. Can anyone help me?

$dt = $xml->item->parameter[2];
$to = date('Y-m-d H:i:s',strtotime($dt));
$query = sprintf("select * from me,val where group_id=%s AND m_id= me_id AND time_stamp <= $to",mysql_real_escape_string($gid));
$result= mysql_query($query) or die(mysql_error());

while ($row=mysql_fetch_array($result)){

The parsing to a date works. Thanks in advance.

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '23:59:59' at line 1

Upvotes: 1

Views: 80

Answers (2)

Quicksilver
Quicksilver

Reputation: 2700

$query = sprintf("select * from me,val where group_id=%s AND m_id= me_id AND time_stamp <= '$to'",mysql_real_escape_string($gid));

or

$query = sprintf("select * from me,val where group_id=%s AND m_id= me_id AND time_stamp <= %s",mysql_real_escape_string($gid),$to);

you should enclose date in single quotes

Upvotes: 1

oezi
oezi

Reputation: 51797

you have to put the date ($to) in single quotes:

"select * from me,val where group_id=%s AND m_id= me_id AND time_stamp <= '$to'"

to avoid these kind (and a lot of other) problems, you might want to think about using prepared statements (take a look at PDO or mysqli) instead of the old (and deprecated) mysql_*-functions.

Upvotes: 3

Related Questions