Jump to content

Working with the last record of a query


senyo

Recommended Posts

Hi

I am trying to get the last record of a query in a variable from a list of 100 records

this is the code I use to get the last 100 records from a db

$query  = "SELECT * FROM entry WHERE entry.ok='1' ORDER BY id DESC LIMIT 100";
   $result = mysql_query($query);

   // Display records from the table
   echo "<table border='1'>";
   while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
      echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td></tr>";
}

 

What should I use so that the record with the number 100 is placed in a variable?

Link to comment
Share on other sites

$query  = "SELECT * FROM entry WHERE entry.ok='1' ORDER BY id DESC LIMIT 100";
   $result = mysql_query($query);

   // Display records from the table
$hundredth_record=array();
$rec_num=1;
   echo "<table border='1'>";
   while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
      echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td></tr>";
      if($rec_num==100){
          $hundredth_record=$row;
      }
     $rec_num++;
}

$hundredth_record will contain the record with the number 100

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.