4

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?

0

5 Answers 5

7

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

3
  • Thanks! But now my next line: $row = mysql_fetch_assoc($result); is unhappy. Again, it works fine when not using the primary key.
    – djs22
    May 28, 2010 at 8:34
  • Isn't giving one,but it's choosing to go the or die route every time...
    – djs22
    May 28, 2010 at 8:40
  • There's no MySQL or PHP error? May 28, 2010 at 8:59
2

'key' is a reserved keyword, put backtick quotes around it:

"SELECT * FROM Bowlers WHERE `key` = '1'"
3
  • 3
    ...and if key is a numeric type, then the literal should NOT have quotes (it'll work - but it'll go much slower)
    – symcbean
    May 28, 2010 at 8:26
  • @symcbean OMG "much slower". You have no idea of slowness May 28, 2010 at 8:35
  • Indeed, it will be optimised away easily, especially as it's the primary key. The difference in timings is negligible. However, I do agree that putting quotes around numeric literals is bad style.
    – mercutio
    May 28, 2010 at 8:55
2

Without checking, it's likely that "key" is a reserved word in MySQL. Try wrapping it in backticks

$query ="SELECT * FROM Bowlers WHERE `key` = '1'";  
1

You should write the column name key in quotes

$query ="SELECT * FROM Bowlers WHERE `key` = '1'";

Otherwise it is a keyword

0

I run into that all the time. MySQL has a crap load of reserved words. And when you come across one, the mysql error function is not even nice enough to let you know what is wrong.

The only thing you can do is change the column name. I accidentally used "date, to and from" the other day. Was pulling my hair out when it dawned on me, DuHH!!! those are DB reserved.

You can wrap all kind of quotes around it, it does not matter when it references a column name. Reserved is reserved!

It is common practice to to do a couple of things.

1) When making tables: Split resource type with resource name using underscore. Example: xref_userMessages

This would mean it is a cross reference table for User messages.

2) Other examples of table names:

msg_Messages | sys_Settings | cli_Logins
So any other table made related to messages would be called msg_??? , not only does this keep them grouped together in phpMyadmin but makes remembering the names easier too.

3) When Making columns: Never use a reserved. Thus causing key columns to always be 6 didgets. Example:

admkey | usrkey | msgkey | clikey grpkey

Obviously Admin Key | User Key | Message Key | Client Key | Group Key

So this means "msg_Messages" keys are "msgkey" and the xref table would be xref_Messages and its keys are xref_msgkey. Following this logic you not only know what to name everything without even thinking about it, but you never run into any reserved words doing it.

4) Examples of Column names:

dateInsert dateStart timeCreate admName admAddress admPhone admCell

Just like above there is a logic to it. Placing purpose/owner and noun/item together makes the name and again avoids reserved words.

Last Example:

Table: users_Admins users_Clients Key: admkey usrkey

Table: msg_Messages Columns: msgkey admkey usrkey msgRead msgMessage msgTitle

Just in this short example I avoided 2 reserved words. Key and Read

So in short, your problem is not reading a primary key. It is a problem with column names. MySQL is seeing your code as having a syntax that has commands out of place. SELECT read ... or SELECT key ... it doesnt matter if you put quotes around it or not. MySQL is basically seeing ...

SELECT (SELECT,WHERE,FROM) FROM select,from,where 
WHERE SELECT = WHERE & FROM = SELECT. hehehehehehehe

Putting a different kind of quote around this will not change the confusion level you just sent to MySQL.

Mixing my mistake and your mistake together looks like this...


SELECT key,from,to,date FROM my_table WHERE key='1';

// Same as...
SELECT SELECT,SELECT,SELECT,SELECT FROM my_table WHERE SELECT='1';

The first one you can't really tell by looking at it there is anything wrong with it. The second one it is obvious that it is not right and won't work. However, according to MySQL they are the SAME THING.

MySQL receives this syntax as so... SELECT? You told me to SELECT 5 times, never told me what to even select. You get the FROM right, but then you ended with a left hook telling to select something else, not only did you not tell me what to select again but you threw in an NULL='1'; what the heck is that all about? This is why when you make these kinds of errors the error function doesn't even report what the heck happened. There were so many errors it can't throw you an error number so it just stops.

So this means your syntax is like this
SELECT * FROM Bowlers WHERE SELECT = '1';

Sometimes I get frustrated and say, "I wish MySQL was smarter than this!!!" But then I realize i would have to trade the key words in for a lesser valued database. Each one of those reserved words represents a word that is doing a whole lot more work on the database side for me. When I first started to learn programming I had to write my own text input field sub routines, so I appreciate all the neat things MySQL does for me.

Not the answer you're looking for? Browse other questions tagged or ask your own question.