Sorting Database Entries

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
Crispin_Java
Forum Newbie
Posts: 8
Joined: Thu Feb 05, 2009 2:34 pm

Sorting Database Entries

Post by Crispin_Java »

Hi,
I have an sql database which I am accessing through PHP, but I can`t figure out how to sort the entries by how recent they are... When I list the entries as shown below, they seem to appear in almost random order:

$theUser = mysql_query("SELECT user FROM Comments");
while($userRow = mysql_fetch_array($theUser))
{
echo $userRow["user"];
}

-----

Any ideas, I`m a little new to this :D
Thanks,
C
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Sorting Database Entries

Post by pickle »

You want to use the SQL ORDER BY clause.

Code: Select all

SELECT 
   * 
FROM 
   `myTable` 
ORDER BY 
   `columnToSortBy`
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Crispin_Java
Forum Newbie
Posts: 8
Joined: Thu Feb 05, 2009 2:34 pm

Re: Sorting Database Entries

Post by Crispin_Java »

Thanks a lot for the answer, I just got it working perfectly :)

One more question, if I may:

I am connecting to different fields in the database the following way:

$theComment = mysql_query("SELECT comment FROM Comments ORDER BY thisorder DESC");
$theUser = mysql_query("SELECT user FROM Comments ORDER BY thisorder DESC");
$theVideo = mysql_query("SELECT video FROM Comments ORDER BY thisorder DESC");
$theSubmitter = mysql_query("SELECT submitter FROM Comments ORDER BY thisorder DESC");

It seems like the database is being sorted four times - is there a way to sort all four fields at the same time? Is seems like that would make it much more efficient.

Cheers,
C
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Sorting Database Entries

Post by jayshields »

Code: Select all

SELECT `comment`, `user`, `video`, `submitter` FROM `Comments` ORDER BY `thisorder` DESC
This is very simple SQL, you could easily find this stuff on Google.
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Sorting Database Entries

Post by Skoalbasher »

jayshields wrote:

Code: Select all

SELECT `comment`, `user`, `video`, `submitter` FROM `Comments` ORDER BY `thisorder` DESC
This is very simple SQL, you could easily find this stuff on Google.
Or you could do

Code: Select all

SELECT * FROM `Comments` ORDER BY `thisorder` DESC
Crispin_Java
Forum Newbie
Posts: 8
Joined: Thu Feb 05, 2009 2:34 pm

Re: Sorting Database Entries

Post by Crispin_Java »

Thanks SkoalBasher,

If I sort the entries as you suggested, like this:
$total = mysql_query("SELECT * FROM `Comments` ORDER BY `thisorder` DESC");

Then how do I put each field into a seperate string?

Cheers,
Crispin
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Sorting Database Entries

Post by Ziq »

I think you should read something about mysql and php cooperation. Firstly read the manual.
Then how do I put each field into a seperate string?
Read about this function mysql_fetch_assoc() or similar.
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Sorting Database Entries

Post by Skoalbasher »

Crispin_Java wrote:Thanks SkoalBasher,
Then how do I put each field into a seperate string?

Cheers,
Crispin
You already know how. :) You did it in the example you provided us.
Post Reply