php help
- Bioprobe
-
Bioprobe
- Member since: Jun. 30, 2001
- Offline.
-
- Forum Stats
- Member
- Level 23
- Blank Slate
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.
-
- Send Private Message
- Browse All Posts (11,644)
- Block
-
- Forum Stats
- Staff
- Level 28
- Blank Slate
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

