Jump to content

Fetch Data from Last Row


xProteuSx

Recommended Posts

I am writing a PHP script that determines the number of rows in a mysql table, then goes to the last row and retrieves data from that row.  I have written a script which determines the number or rows, but how do I get it to read data from the last row specifically?

Link to comment
https://forums.phpfreaks.com/topic/97676-fetch-data-from-last-row/
Share on other sites

So, if $numberofrows is the number of rows in a table I can get the data as follows??

 

$specificresult = mysql_query(mysql_data_seek($numberofrows)) or die (mysql_error());

 

I have never used this function before, and the official manual is confusing the crap out of me. :(

no, it would be:

 

<?php
  $sql = "SELECT * FROM `tablename` ORDER BY `somefield`";
  $result = mysql_query($sql);
  $num = mysql_num_rows($result);
  if($num){
    mysql_data_seek($result,$num-1)
    $last = mysql_fetch_array($result);

    echo $last['somefield'];
  }
?>

rhodesa, the code you have given me does not seem to function (even when I put the missing ';' following 'mysql_data_seek($result,$num-1)'.  I have a couple of questions, if you don't mind.

 

First of all, what does 'if($num)' do?  That does not seem like a proper boolean argument to me.  Am I missing something?  Second, '$last['handle]' comes back blank.  I have changed the tablename.

 

My table is entitled 'users' and I am looking to get the value of the 'handle' column for the last row.

 

I have it as ...

 

<?php

  $sql = "SELECT * FROM `users` ORDER BY 'id'";

  $result = mysql_query($sql);

  $num = mysql_num_rows($result);

  if($num){

    mysql_data_seek($result,$num-1)

    $last = mysql_fetch_array($result);

 

    echo 'The last registered member is: ' . $last['handle'];

  }

?>

if you want the actual last record by somefield, just sort in desc and take the first record.

 

<?php
  $sql = "SELECT * FROM `tablename` ORDER BY `somefield` DESC";
  $result = mysql_query($sql) or die(mysql_error());
  $number_of_recs = mysql_num_rows($result);
  if ($number_of_recs) {
      $last = mysql_fetch_array($result);
      echo $last['somefield'];
  } else {
      echo "no records."
  }
?>

 

"what does 'if($num)' do?"

 

it's true if $num is greater than zero.

if you want the actual last record by somefield, just sort in desc and take the first record.

 

<?php
  $sql = "SELECT * FROM `tablename` ORDER BY `somefield` DESC";
  $result = mysql_query($sql) or die(mysql_error());
  $last = mysql_fetch_array($result);
  echo $last['somefield'];
?>

 

"what does 'if($num)' do?"

 

it's true if $num is greater than zero.

 

Agreed, with your stuff it would be:

 

<?php
  $sql = "SELECT * FROM `users` ORDER BY `id` DESC";
  $result = mysql_query($sql) or die(mysql_error());
  $last = mysql_fetch_assoc($result);
  echo 'The last registered member is: ' . $last['handle'];
?>

Right on!  That did it.  I have yet to learn about 'mysql_fetch_assoc' so that's why I could not figure this out on my own.  Thanks very much.  This topic is now SOLVED ... speaking of which, wasn't there a 'Solved' button somewhere before??

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.