Display last three entries in a table

Discussion in 'PHP' started by philb, Oct 24, 2011.

  1. #1
    I want to show just the last three entries from a mysql database.

    I have the code to show the full table but could do with some help to tweak it to show just the last 3 entries from the table.

    Can anyone help me out?

    It's probably pretty easy for you php guys :)

    //connection to the database
    $dbhandle = mysql_connect($hostname, $username, $password) 
      or die("Unable to connect to MySQL");
    
    
    //select a database to work with
    $selected = mysql_select_db("#####",$dbhandle) 
      or die("Could not select examples");
    
    //execute the SQL query and return records
    $result = mysql_query("SELECT * FROM readersubmit ORDER BY id DESC");
    
    
    
    //fetch tha data from the database 
    while ($row = mysql_fetch_array($result)) {
    
       echo "<p>".$row['submit']. "</p>"; //display the results
       
    }
    //close the connection
    mysql_close($dbhandle);
    PHP:

     
    Solved! View solution.
    philb, Oct 24, 2011 IP
  2. #2
    Just Replace
    //execute the SQL query and return records
    $result = mysql_query("SELECT * FROM readersubmit ORDER BY id DESC");

    with
    //execute the SQL query and return records
    $result = mysql_query("SELECT * FROM readersubmit ORDER BY id DESC LIMIT 0,3");
     
    muslim_ansari, Oct 24, 2011 IP
  3. philb

    philb Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks very much muslim_ansari that worked a treat.
     
    philb, Oct 24, 2011 IP