Jump to content


Photo

Weird sql query problem


  • Please log in to reply
3 replies to this topic

#1 Payback451

Payback451
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 17 June 2006 - 12:31 AM

I've spent quite a bit of time looking at this for the problem, and i'm completely stumped.

$Messages = mysql_query("SELECT * FROM pm WHERE to = $UserInfo[id]") or die(mysql_error());
while($Message = mysql_fetch_array($Messages)) {
[display code- no problems here]
}

Produces the following 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 'to = 1' at line 1

$UserInfo[id] is properly set to the number 1. There are 3 entries in the table pm that should be selected. Why is this producing an error? This has also happened in several other places in my script.. and the code is fine as far as I and my other coder friends can see. Was there some major update to the way mysql and php interact that would cause this to give a sytnax error? Because i have never experienced anything like this in the past.

Also, to save you all time, i've tried putting the $UserInfo[id] in quotes, tried using mysql_fetch_row, tried changing the variable name, tried selecting specific fields instead of *, tried renaming the table, and some more.

Thanks very much for your help.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 17 June 2006 - 12:53 AM

to is a reserved word in MySQL and cannot be used as a field name. You ought to rename that table field something else ... or enclose it with backticks `to` [which IMO is the worst solution]
Legend has it that reading the manual never killed anyone.
My site

#3 Payback451

Payback451
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 17 June 2006 - 01:21 AM

I feel very stupid for not realizing that.. especially when my querys containing the field name 'desc' (for description) didnt work.. cmon.. I use DESC and ASC all the time.. shoulda known.

Anyway, thank you very much!

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 17 June 2006 - 01:46 AM

All's well that ends well :)
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users