xProteuSx Posted March 24, 2008 Share Posted March 24, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/97676-fetch-data-from-last-row/ Share on other sites More sharing options...
rhodesa Posted March 24, 2008 Share Posted March 24, 2008 By 'script which determines the number of rows' I hope you are using mysql_num_rows() To move to a specific row, use mysql_data_seek() Quote Link to comment https://forums.phpfreaks.com/topic/97676-fetch-data-from-last-row/#findComment-499789 Share on other sites More sharing options...
xProteuSx Posted March 24, 2008 Author Share Posted March 24, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/97676-fetch-data-from-last-row/#findComment-499798 Share on other sites More sharing options...
rhodesa Posted March 24, 2008 Share Posted March 24, 2008 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/97676-fetch-data-from-last-row/#findComment-499811 Share on other sites More sharing options...
frijole Posted March 24, 2008 Share Posted March 24, 2008 thats a handy little function. Quote Link to comment https://forums.phpfreaks.com/topic/97676-fetch-data-from-last-row/#findComment-499819 Share on other sites More sharing options...
Barand Posted March 24, 2008 Share Posted March 24, 2008 Most efficient way to get number of rows in a table is SELECT COUNT(*) FROM tablename Quote Link to comment https://forums.phpfreaks.com/topic/97676-fetch-data-from-last-row/#findComment-499823 Share on other sites More sharing options...
sureshp Posted March 24, 2008 Share Posted March 24, 2008 Yes -- COUNT will be the best way to retrieve the no. of rows. Others taking time to execute where COUNT is not taking time to execute it in phpmyadmin . Quote Link to comment https://forums.phpfreaks.com/topic/97676-fetch-data-from-last-row/#findComment-499842 Share on other sites More sharing options...
xProteuSx Posted March 24, 2008 Author Share Posted March 24, 2008 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/97676-fetch-data-from-last-row/#findComment-499866 Share on other sites More sharing options...
BlueSkyIS Posted March 24, 2008 Share Posted March 24, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/97676-fetch-data-from-last-row/#findComment-499879 Share on other sites More sharing options...
rhodesa Posted March 24, 2008 Share Posted March 24, 2008 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/97676-fetch-data-from-last-row/#findComment-499880 Share on other sites More sharing options...
xProteuSx Posted March 24, 2008 Author Share Posted March 24, 2008 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?? Quote Link to comment https://forums.phpfreaks.com/topic/97676-fetch-data-from-last-row/#findComment-499898 Share on other sites More sharing options...
rhodesa Posted March 24, 2008 Share Posted March 24, 2008 mysql_fetch_array would have gotten you the same result, i just prefer _assoc Quote Link to comment https://forums.phpfreaks.com/topic/97676-fetch-data-from-last-row/#findComment-499908 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.