1

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks at first, it fixed that problem but brought me a knew. ^^ To be honest, I already had PDO in use but I couldn't find out how this "($row=mysql_fetch_array($result)" has to look like in PDO. May you can help me?
$row = $result_object->fetch()
1
$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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.