Page 1 of 1
How Do I Sort A MySQL Database
Posted: Thu Sep 22, 2005 2:31 pm
by icesolid
I want to be able to sort my MySQL database by a
NON primary key.
Example:
Code: Select all
<?php
mysql_query("SELECT * FROM table ORDER BY order ASC");
?>
In this case the field
order is an alpha field (a, b, c, d, e, f, etc.).
Posted: Thu Sep 22, 2005 2:46 pm
by feyd
have you tried it?
and please start posting in the correct boards....
I have
Posted: Thu Sep 22, 2005 2:55 pm
by icesolid
I have tried. I get an error, basically telling me that the order field cannot be used to sort.
So I currently use my id primary key field to sort, but I want to give my users the able to set the sort order.
Posted: Thu Sep 22, 2005 5:48 pm
by Jean-Yves
"ORDER" is a reserved word in SQL. Try renaming your field to "SortOrder" or something similar.
Posted: Thu Sep 22, 2005 11:33 pm
by Bill H
Or put the field name in single quotes in the query.
Code: Select all
<?php
mysql_query("SELECT * FROM table ORDER BY 'order' ASC");
?>
Posted: Thu Sep 22, 2005 11:44 pm
by feyd
ahem, backticks... not quotes.
Posted: Fri Sep 23, 2005 8:49 am
by Bill H
oops. I know that backticks are the usual specifier for bracketing field and table names. But single quotes work perfectly well for this application, actually. After reading your post I had to go to phpmyadmin to check my memory, and try it in a line of code, since I've been avoiding the use of reserved words. (Which is actually the better practice anyway.)
Code: Select all
$Result = mysql_query("SELECT Pass FROM Admin WHERE id=1 ORDER BY 'order'", $Link);
That line of code executed fine with single quotes. MySQL is very forgiving (up to a point).
Posted: Sat Sep 24, 2005 2:33 pm
by Jean-Yves
Personally, I'd advise against starting off by adopting bad habits such as using reserved words for field names. You're just storing up problems for later.