views:

21

answers:

3

Due to some help from a recent post, I'm selecting a row by primary key, as follows:

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

For some reason, the third line of code dies every time, without error. It works fine using other keys, ie WHERE name = 'djs22'.

Any ideas?

+1  A: 

You are using single quotes on the field name, you must use backticks.

not ', but `
mercutio
Oh wow - that's actually the same fix from my last post. This time it just crashed in a different spot so I thought I had implemented it correctly. Thanks for teaching me the difference between a single quote and a backtick, definitely should've realized that before!
djs22
Notes that the backtick is a MySQL-specific invention: in standard ANSI SQL it should be double-quotes, but MySQL incorrectly uses that as an alternative for string literals by default. If you need to be cross-database compatible, avoid quoting field and table names (which does mean you have to be careful with what you name them). This is quite unfortunate.
bobince
A: 

try

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

or

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

instead of

$query ="SELECT * FROM Bowlers WHERE 'key' = '1'";
oezi
In this case, ``key`\` would be the only option, as key is a reserved word in mysql
Marc B
A: 

try using this

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

I just replaced ' ' by .

Vimard