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
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. :(

Link to comment
Share on other sites

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'];
  }
?>

Link to comment
Share on other sites

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'];

  }

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'];
?>

Link to comment
Share on other sites

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??

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.