Be a Supporter!

php help

  • 268 Views
  • 2 Replies
New Topic Respond to this Topic
Bioprobe
Bioprobe
  • Member since: Jun. 30, 2001
  • Offline.
Forum Stats
Member
Level 23
Blank Slate
php help 2002-03-25 19:02:30 Reply

I am trying to write a script that would pull data from a mysql db, sort the data, and then output it. I have the part where the script connects to the db and selects the table to use. The trouble is that I don't know how to sort the data. I need a loop to pull all the data out and then it needs to be sorted.
help.

liljim
liljim
  • Member since: Dec. 16, 1999
  • Offline.
Forum Stats
Staff
Level 28
Blank Slate
Response to php help 2002-03-26 07:22:53 Reply

At 3/25/02 07:02 PM, Bioprobe wrote: I am trying to write a script that would pull data from a mysql db, sort the data, and then output it. I have the part where the script connects to the db and selects the table to use. The trouble is that I don't know how to sort the data. I need a loop to pull all the data out and then it needs to be sorted.
help.

You'll need to user ORDER BY in your select statement. Assuming, for example, you have a field in the table "mytable" called "username" and you wanted to list all of your users alphabetically, you'd use:

"SELECT username FROM mytable ORDER BY username ASC";

ASC - Ascending
DESC - Descending

to loop through the result set in php, you'd use a while or for loop in conjunction with mysql_fetch_array(), mysql_fetch_assoc(), or one of the row or object (or other associated) functions.

Something like:

while($row = mysql_fetch_array($query_identifier)) {
echo $row['username'] . "<br>";
}

Also see the mysql manual entry for select:
http://www.mysql.com/doc/S/E/SELECT.html

Bioprobe
Bioprobe
  • Member since: Jun. 30, 2001
  • Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to php help 2002-03-27 20:40:16 Reply

Thanks for the help. You know what you are doing.