[PHP/MySql] Sorting After Select

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by iamcameron, Jan 13, 2010.

  1. iamcameron

    iamcameron New Member

    Messages:
    18
    Likes Received:
    0
    Trophy 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
     
  2. slacker3

    slacker3 New Member

    Messages:
    146
    Likes Received:
    6
    Trophy 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: Jan 13, 2010
  3. vishal

    vishal -::-X10 Guru-::- Community Support

    Messages:
    5,255
    Likes Received:
    192
    Trophy Points:
    63
    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
     
  4. descalzo

    descalzo Grim Squeaker Community Support

    Messages:
    9,375
    Likes Received:
    327
    Trophy 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.
     

Share This Page