jim.davidson Posted October 12, 2009 Share Posted October 12, 2009 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 More sharing options...
Maq Posted October 12, 2009 Share Posted October 12, 2009 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 https://forums.phpfreaks.com/topic/177427-need-to-get-info-from-last-record/#findComment-935507 Share on other sites More sharing options...
naskoo Posted October 12, 2009 Share Posted October 12, 2009 http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html Link to comment https://forums.phpfreaks.com/topic/177427-need-to-get-info-from-last-record/#findComment-935508 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.