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

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.