How To Sort From the Latest to Oldest Data PHP / MySQL

Discussion in 'PHP' started by extraspecial, Sep 7, 2012.

  1. #1
    Hello guys I'm wondering whether someone can help me to change the sorting from oldest to latest data into latest to oldest data sorting?

    I have a database of users which are added after they fill a form, and I created a page where I can see these users from the database but I'm always seeing starting from the first ID to latest so what I want to do is to change to latest ID to oldest data, here is my code can someone help?

    Thanks

    <?php
    mysql_connect("localhost", "user", "password") or die ("Problem with Connection...");
    mysql_select_db("database");
    $per_page = 20;
    $pages_query = mysql_query("SELECT COUNT('id') FROM table");
    $pages = ceil(mysql_result($pages_query, 0) / $per_page);
    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
    $start= ($page - 1) * $per_page;
    $query = mysql_query("SELECT * FROM users LIMIT $start, $per_page");

    Then I echo the values on a table format starting with the first ID....
    echo "<table width=\"90%\">";
    echo "<tr><th>ID</th>
    .....
     
    Solved! View solution.
    extraspecial, Sep 7, 2012 IP
  2. #2
    $query = mysql_query("SELECT * FROM users LIMIT $start, $per_page ORDER BY ID DESC");
    PHP:
    Think this should do it
     
    plussy, Sep 7, 2012 IP
  3. extraspecial

    extraspecial Member

    Messages:
    788
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    45
    #3
    Thanks buddy, I needed to change it a little bit but I got it now :)

    $query = mysql_query("SELECT * FROM users ORDER BY id DESC LIMIT $start, $per_page");
     
    extraspecial, Sep 7, 2012 IP