quick MySQL question
- Started
- Last post
- 7 Responses
- dsp
i'm trying to sort a database according to a certain variable, and have the results shown in a descending order.
This doesn't work however:
mysql_query( "SELECT * FROM news WHERE type = $xxx DESC" )This does show the results in descending order, but it shows everything in the database:
mysql_query( "SELECT * FROM news ORDER BY id DESC;" )any ideas?
thanks
- GooseAss0
i could be wrong but i think what you want is "SELECT * FROM news WHERE type=$type ORDER BY type DESC"
pretty sure that DESC requires an order by clause first, otherwise how would it know where to start descending?
- dsp0
argh, it doesn't work. It shows the proper items, but for some reason the DESC isn't affecting it. I'm still getting the results in ascending order.
Thanks though!
- mike0
you need the name of the colum you wish to sort by decending...
"SELECT * FROM news WHERE type=$type ORDER BY type DESC"
also, if type is a non-intger value you need to put single quotes around it... like...
"SELECT * FROM news WHERE type='$type' ORDER BY type DESC"
- dsp0
very odd, neither statements are having any affect. The database is sorted according to $type, but still ascendingly. If anyone would like to take a peek at the code (PHP), it can be found here:
- dsp0
that's just the related segment mind you, not the whole script.
thanks in advanced.
- mike0
doug, remove the semi-colon at the end of your sql statement inside of quotes... it is not needed.
so the line should end ORDER BY type DESC"
and again, if the type field is anything other than an integer you need to wrap single quotes around your compared value...
type='xxx'
- dsp0
thanks mike, your a champion!