Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP/MYSQL Trouble Selecting by Primary Key [duplicate]

So I have a primary key column called key. I'm trying to select the row with key = 1 via this code:

$query ="SELECT * FROM Bowlers WHERE key = '1'"; 
$result = mysql_query($query) or die(mysql_error());

For some reason, I'm getting this result:

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 = '1'' at line 1

The mysql statement works for using other keys, ie WHERE name = 'djs22'.

Any ideas?

like image 309
djs22 Avatar asked Dec 18 '22 00:12

djs22


1 Answers

key is a reserved word, try putting ticks around it:

$query ="SELECT * FROM `Bowlers` WHERE `key` = '1'"; 
$result = mysql_query($query) or die(mysql_error());

To see all the reserved words, go here and scroll down: http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

like image 177
Kerry Jones Avatar answered Dec 28 '22 06:12

Kerry Jones