1

I get a error message saying:

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 'key='12345' AND id='98765' LIMIT 1' at line 1

My code is:

$key = '12345'; 
$id = '98765';  
include realpath('./inc/config.php');

            $query = mysql_query("SELECT * FROM users WHERE key='{$key}' AND id='{$id}' LIMIT 1", $config) or die(mysql_error());
            $result = mysql_fetch_assoc($query);

Now can anyone tell me whats wrong in this?

Adam Wenger
  • 17,100
  • 6
  • 52
  • 63
Keshav Nair
  • 423
  • 1
  • 14
  • 24
  • I am not certain but are you sure you have used proper quotes for key (it appears to me as non-digit data)??? – Umer Nov 24 '11 at 05:39
  • why are you putting braces with the variables ? – Arfeen Nov 24 '11 at 05:39
  • what are the datatypes for the fields key and id ??? – ping localhost Nov 24 '11 at 05:39
  • Damn sorry guys key is a predefined function in mysql or something like that you know primary key and all so it gives and error i changed it from key to keys and it worked and its not necessary to put braces?? EDIT: No sorry anything except key even keys are not working – Keshav Nair Nov 24 '11 at 05:45

3 Answers3

9

key is a reserved word, you need to properly quote it with backticks if you want to use it as a field name.

SELECT * FROM users WHERE `key`='{$key}' AND id='{$id}' LIMIT 1
Maerlyn
  • 33,687
  • 18
  • 94
  • 85
1
SELECT * FROM users WHERE `key`='{$key}' AND id='{$id}' LIMIT 1

key is a reserved word

Ghostman
  • 6,042
  • 9
  • 34
  • 53
-2

Your key and id are obviously numeric. Although adding quotes wouldn't hurt, you definitely don't need them. You edon't need brackets in any query period. Try this:

        $query = mysql_query("SELECT * FROM users WHERE key=$key AND id=$id LIMIT 1", $config) or die(mysql_error());
        $result = mysql_fetch_assoc($query);

If that doesn't work just run this using PHPMyAdmin or whatever you use to run queries on your db.

         SELECT * FROM users WHERE key=12345 AND id=98765

I also don't see why you would need LIMIT clause. It wouldn't break anything but if your id is actually row id it should give you a unique record.

AR.
  • 1,935
  • 1
  • 11
  • 14