Reputation: 13
I have a php script that pulls content from a database and prints them in a certain fashion. The database has a column-header called "order" which is an INT size 11. I'm trying to order the contents by the value "order" in the database when I'm getting data from it, like this:
<?php
$db_hostname = '<hostname>';
$db_database = '<db>';
$db_username = '<username>';
$db_password = '<password>';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
mysql_select_db($db_database, $db_server);
$query = "SELECT * FROM <table> ORDER BY order"; // why is "ORDER BY order" problematic...
$table = mysql_query($query);
$data_items = '';
$carousel_items = '';
while($row = mysql_fetch_array($table)) {
// ...etc
There are few rows in the database I'm getting information from, and the query "SELECT * FROM <table>"
works exactly the way it should. What am I doing wrong?
If it helps, the error I'm getting back (on the website that this script is for):
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /$PATH/php/myfile.php on line 15
Line 15 is while($row = mysql_fetch_array($table)) {
I'm aware that if I wasn't lazy I could sort this out without the MySQL query and, because php is absurdly flexible, do something like $array[$row['order']] = $row['what I want'];
, but I'd like a solution that doesn't have to add those (what should be) unnecessary lines. I've also tried adding the semicolon (just to be sure) to the end of my query, but it doesn't change anything at all.
Thanks for any help!
Upvotes: 0
Views: 70
Reputation: 377
Order is a reserved word in mysql. Use backticks to surround the reserved word like this:
"ORDER BY `order`"
Documentation here:
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
Upvotes: 0
Reputation:
Since order
is a keyword you need to enclose it in backticks if you want to use it as an identifier:
SELECT * FROM <table> ORDER BY `order`
Upvotes: 0
Reputation: 97672
order
is a reserved word, surround it with backticks
$query = "SELECT * FROM <table> ORDER BY `order`";
Upvotes: 5