Grabing the 5 latest rows

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lenard Redwood

    Grabing the 5 latest rows

    I am trying to create a PHP script that queries entries from a MySQL
    table:

    <?php
    $user="myuserna me";
    $password="mypa ssword";
    $database="myda tabase";
    mysql_connect(l ocalhost,$user, $password);
    @mysql_select_d b($database) or die( "Unable to select database");
    $query = "SELECT ititle, inumber FROM nucleus_item";
    $result = mysql_query($qu ery);

    while($row = mysql_fetch_arr ay($result, MYSQL_ASSOC))
    {
    echo "Name :{$row['ititle']} <br>" .
    "Subject : {$row['inumber']} <br>";
    }
    mysql_close();
    ?>

    What I'd like to do is to grab the 5 latest rows of the "inumber"
    field. This field starts from 1 and currently ends at 2100 but everyday
    there are new entries. So I think I should sort the table rows from the
    greatest number to the lowest, and take only the 5 latest entries. I
    didn't find on php.net how to do that. Any idea?
    Thanks,

    Lenard.

  • frizzle

    #2
    Re: Grabing the 5 latest rows


    Lenard Redwood wrote:
    I am trying to create a PHP script that queries entries from a MySQL
    table:
    >
    <?php
    $user="myuserna me";
    $password="mypa ssword";
    $database="myda tabase";
    mysql_connect(l ocalhost,$user, $password);
    @mysql_select_d b($database) or die( "Unable to select database");
    $query = "SELECT ititle, inumber FROM nucleus_item";
    $result = mysql_query($qu ery);
    >
    while($row = mysql_fetch_arr ay($result, MYSQL_ASSOC))
    {
    echo "Name :{$row['ititle']} <br>" .
    "Subject : {$row['inumber']} <br>";
    }
    mysql_close();
    ?>
    >
    What I'd like to do is to grab the 5 latest rows of the "inumber"
    field. This field starts from 1 and currently ends at 2100 but everyday
    there are new entries. So I think I should sort the table rows from the
    greatest number to the lowest, and take only the 5 latest entries. I
    didn't find on php.net how to do that. Any idea?
    Thanks,
    >
    Lenard.
    Be sure to set what field to order by, what order (DESC/ASC), and to
    limit it to five, use LIMIT=5

    Good luck!

    Comment

    • Erwin Moller

      #3
      Re: Grabing the 5 latest rows

      Lenard Redwood wrote:
      I am trying to create a PHP script that queries entries from a MySQL
      table:
      >
      <?php
      $user="myuserna me";
      $password="mypa ssword";
      $database="myda tabase";
      mysql_connect(l ocalhost,$user, $password);
      @mysql_select_d b($database) or die( "Unable to select database");
      $query = "SELECT ititle, inumber FROM nucleus_item";
      $result = mysql_query($qu ery);
      >
      while($row = mysql_fetch_arr ay($result, MYSQL_ASSOC))
      {
      echo "Name :{$row['ititle']} <br>" .
      "Subject : {$row['inumber']} <br>";
      }
      mysql_close();
      ?>
      >
      What I'd like to do is to grab the 5 latest rows of the "inumber"
      field. This field starts from 1 and currently ends at 2100 but everyday
      there are new entries. So I think I should sort the table rows from the
      greatest number to the lowest, and take only the 5 latest entries. I
      didn't find on php.net how to do that. Any idea?
      Hi,

      You don't find it at php.net because you should have searched your mysql
      documentation. :-)
      The keyword you are looking for is LIMIT

      example:
      SELECT firstname, lastname FROM tblusers ORDER BY lastname DESC LIMIT 10

      Regards,
      Erwin Moller

      Thanks,
      >
      Lenard.

      Comment

      • Lenard Redwood

        #4
        Re: Grabing the 5 latest rows


        Erwin Moller wrote:
        SELECT firstname, lastname FROM tblusers ORDER BY lastname DESC LIMIT 10
        Thanks, it's so simple... Problem is finding where in the docs... But
        this worked very well, thanks.

        Comment

        Working...