0

I have tried so many different soloutions but cannot get this to work Here is my code:

$to = $_POST['to'];
$query = "SELECT to FROM to WHERE to='$to' " 
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)){

I get a whole load of different errors every time i modify it. At the moment I'm getting

You have an error in your SQL syntax near to='Name'

When I modify it to fix this I get

mysql_fetch_array() not valid

It seems when using variables it messes up

can anyone help?

Thanks!

Calum
  • 5,308
  • 1
  • 22
  • 27
ApiMail
  • 161
  • 3
  • 10

3 Answers3

5

to is a reserved word in mySQL.

You would have to wrap each mention of the table or column name into backticks

SELECT `to` from `to`

but it would be vastly better to use a different name.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Thanks, that seems to have mostly solved it. But i now have this code $usersent = $_POST['usersent']; $query = "SELECT * FROM message WHERE usersent='$usersent' "; $result = mysql_query($query) or die(mysql_error()); and if i enter usersent='TheName' "; it all works find but when using the variable i dont get a response, i type in TheName on my html but it comes up no results – ApiMail Apr 18 '11 at 09:07
3

To is a Reserved keyword try escaping it by using "``" symbol

Check this Link

Reserved Keywords MYSQL

Harish
  • 2,311
  • 4
  • 23
  • 28
2

Consider changing the names of your field and table (Edit: Definitely change the names or at least escape them.) Also, all you are doing is selecting the variable you already have.

Calum
  • 5,308
  • 1
  • 22
  • 27