Jump to content

Need to get info from last record


jim.davidson

Recommended Posts

I'm pretty new at this and learning as I go.  What I want to do is sort the table on ref_num, go to the last record and get that number.

 

I'm using MySQL 4, here's what I got

 

  // want the ref_num of the last record

  mysql_select_db($database_mine, $mine);

  $query_getLastRef = sprintf("SELECT * FROM books ORDER BY ref_num ASC",);

  $getLastRef = mysql_query($query_getLastRef, $mine) or die(mysql_error());

  $row_getLastRef = mysql_fetch_assoc($getLastRef);

  $totalRows_getLastRef = mysql_num_rows($getLastRef);

 

 

Am I even in the right ballpark? Any help will be appreciated!

 

Just a last minute thought, would I be better off sorting descending an going to the first record?

Link to comment
https://forums.phpfreaks.com/topic/177427-need-to-get-info-from-last-record/
Share on other sites

If you want to retrieve the last value for 'ref_num' then you have to use a combination of ORDERY BY and LIMIT.  i.e.

 

SELECT * FROM books ORDER BY ref_num DESC LIMIT 1

 

I'm not sure why you're using 'mysql_num_rows()' when you only want to retrieve a single record.  It's always going to be 1, assuming there's no errors and at least 1 record exists.  You also have to use the '$row_getLastRef' associative array to extract the value.

 

$row_getLastRef['ref_num']

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.