I'm having some real trouble running some basic queries that I should be able to run easily.

I have a table called settings. When I query SELECT * FROM settings I get the output:

+----+-----------------+--------------------------------+
| ID | key             | value                          |
+----+-----------------+--------------------------------+
|  1 | view            | default                        |
|  2 | sitename        | value!                    |
|  3 | siteurl         |                                |
|  4 | captcha.enabled | 0                              |
|  5 | upload_path     | http://web/uploads |
+----+-----------------+--------------------------------+

However, when I then run the simply query SELECT key FROM settings WHERE ID > 0 I get an error!

"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 from settings where ID > 0' at line 1
mysql>"

If I wrap key in single quotes, to make: SELECT 'key' FROM settings WHERE ID > 0 I get:

+-----+
| key |
+-----+
| key |
| key |
| key |
| key |
| key |
+-----+

So for the life of me I cannot understand why mySQL won't recognise the column name! The same thing happens for every column name as well, not just key.

If anyone knows what's going on here please let me know.

Recommended Answers

All 2 Replies

Key is a reserved word in MySQL. You need to use back ticks to escape it rather than quotes.
Backticks are the key to the left of the 1 on most keyboards, with the tilde character: ~ as the shift option.

Ah - great to know. Thanks for this!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.