Sort By descending

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Sort By descending

Post by nickman013 »

Hello,

I need help with my query. I know how to sort stuff by descending order, but I cant seem to get it into my query so it will work.

This is my query.

Code: Select all

$sql4 = "SELECT * FROM `comments` WHERE who =".$row['Number'];
I just need to add ORDER BY descending, but I cant figure it out because of the concated stuff.

Thanks!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
$sql4 = "SELECT * FROM `comments` WHERE who =".$row['Number']." ORDER BY who DESC";
?>
Will this do it?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

concatenate some more..

Code: Select all

$sql4 = "SELECT * FROM `comments` WHERE who =" . $row['Number'] . "ORDER BY `foo` DESC";
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Batman's trusty sidekick, Robin wrote:Holy fidgety fingers Batman, did I post before Feyd.
Stephen Hawking wrote:I do believe the space-time continuum, as we know it this day, may be forever imbalanced.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

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 'BY `id` DESC' at line 1

Hmmm
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Post your query again, exactly as it is being called.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Code: Select all

$sql4 = "SELECT * FROM `comments` WHERE who =" . $row['Number'] . "ORDER BY `id` DESC";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

might need a space before the ORDER bit.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Thank you both!!!!
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

A general good habit to get into is to always put a space before and after SQL snippets. So... it becomes:

Code: Select all

$sql4 = " SELECT * FROM `comments` WHERE who = " . $row['Number'] . " ORDER BY `id` DESC ";
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

This is a bit off topic, but it looks like you are using nested query calls -- bad bad bad.

If you would like more help on this, make a post in the Database forums :wink:
Post Reply