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
https://forums.phpfreaks.com/topic/204476-working-with-the-last-record-of-a-query/
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.