+ Reply to Thread
Results 1 to 4 of 4

Thread: [PHP/MySql] Sorting After Select

  1. #1
    iamcameron is offline x10Hosting Member iamcameron is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    18

    Post [PHP/MySql] Sorting After Select

    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 Code:
    $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. #2
    slacker3 is offline x10 Sophmore slacker3 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    146

    Re: [PHP/MySql] Sorting After Select

    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 by slacker3; 01-13-2010 at 10:32 AM. Reason: .

  3. #3
    vishal's Avatar
    vishal is offline -::-X10 Guru-::- vishal has a brilliant futurevishal has a brilliant future
    Join Date
    Nov 2009
    Location
    INDIA
    Posts
    5,254

    Re: [PHP/MySql] Sorting After Select

    Quote Originally Posted by iamcameron View Post
    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 Code:
    $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]
    Regards ~ Vishal
    Giving Reputation (at bottom of my post ) is the best way to encourage the person who helped you on forums.

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: [PHP/MySql] Sorting After Select

    Lacks error checking and using mysql_XXX rather than mysqli_XXX or PDO:

    PHP Code:
    $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.
    Nothing is always absolutely so.

+ Reply to Thread

Similar Threads

  1. Sorting and filtering functions
    By gptsven in forum Review My Site
    Replies: 2
    Last Post: 10-04-2009, 02:28 PM
  2. MySQL SELECT Query question
    By stevet70 in forum Programming Help
    Replies: 2
    Last Post: 06-17-2009, 10:44 AM
  3. Passing a value from a select input field using php
    By unionguy in forum Programming Help
    Replies: 4
    Last Post: 02-16-2009, 12:54 PM
  4. [PHP] sorting 2-dimensional array by timestamp-key
    By bonzo meier in forum Programming Help
    Replies: 0
    Last Post: 02-19-2008, 04:28 PM
  5. I can't select my textarea.
    By Yvette in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 10-30-2007, 10:24 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers