[PHP/MySql] Sorting After Select

iamcameron

New Member
Messages
18
Reaction score
0
Points
0
Hello, My name is Cameron, I have searched the internet for a solution, but i can only find how to sort a mysql Select command when running the SQL command.

I need to reverse a mysql result of 20 rows but i don't know how i would do it, Here is my current SQL command
PHP:
$select=mysql_query("SELECT * FROM table_name ORDER BY id DESC LIMIT 20");
This selects the last 20 rows in the table_name table starting from the last

I need to reverse the result so the last row select is now first, but i dont know how i could do this, Any Ideas?

~~Thanks, Cameron
 

slacker3

New Member
Messages
146
Reaction score
6
Points
0
what happens when you leave out "DESC" ?

as far as i know this keyword already reverses your result
(but i'm no SQL guru :) )
 
Last edited:

vishal

-::-X10 Guru-::-
Community Support
Messages
5,255
Reaction score
192
Points
63
Hello, My name is Cameron, I have searched the internet for a solution, but i can only find how to sort a mysql Select command when running the SQL command.

I need to reverse a mysql result of 20 rows but i don't know how i would do it, Here is my current SQL command
PHP:
$select=mysql_query("SELECT * FROM table_name ORDER BY id DESC LIMIT 20");
This selects the last 20 rows in the table_name table starting from the last

I need to reverse the result so the last row select is now first, but i dont know how i could do this, Any Ideas?

~~Thanks, Cameron
Remove LIMIT ,Limit is used to show particular num of rows .In ur case desc works perfectly and also limits it by 20 so ur r getting only last 20.
Here is a syntax
The syntax for an ORDER BY statement is as follows:
SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC, DESC]
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Lacks error checking and using mysql_XXX rather than mysqli_XXX or PDO:

PHP:
$select=mysql_query("SELECT * FROM table_name ORDER BY id DESC LIMIT 20");
$results = array() ;
while( $one_row=  mysql_fetch_array ( $select  ) ){
   $results[] = $one_row ;
}
$results = array_reverse( $results ) ;

$results should be an array of arrays in the order you want.
 
Top