i am using ORDER BY in mysql SELECT query but i dont know ots not ordering the data.. if i use this query its showing the table but not ordering the data in ascending order
$result = mysql_query("SELECT *FROM learningmaterial ORDER BY 'order' ASC")or die(mysql_error());
but if i use
$result = mysql_query("SELECT *FROM learningmaterial ORDER BY order ASC")or die(mysql_error());
then it give error that the syntax of the query is not right…i’ve seen on various sites but i couldnot found anything unique in my code…i think its right,…please check the query and mend a solution. Thankx in advance 🙂
You need backticks, not single quotes (a):
By using single quotes, you’re ordering the rows by a constant (each row gets the same constant) so effectively not ordering them at all.
By using a “naked” column name of
order, you’re confusing the SQL parser, sinceorderis a reserved word.(a): Of course, this problem goes away if you stop using reserved words as column names but I assume you did that for a reason (such as a bucket-load of programs already depending on the fact that the column is called
order).Myself, I tend not to use generic names for columns (such as
orderordate), preferring instead things that don’t conflict with the language (such asorder_numorstart_date). That way I don’t have to worry about escaping.