Canman2005 Posted March 27, 2009 Share Posted March 27, 2009 Hi all I have the following QUERY $sql = "SELECT * FROM content ORDER BY id ASC"; $show = @mysql_query($sql,$connection) or die(mysql_error()); while ($row = mysql_fetch_array($show)) { print $row['id'].'<br>'; } Is it possible to grab the ID of the first row in the table and the last row in the table? Thanks Link to comment https://forums.phpfreaks.com/topic/151376-solved-getting-first-and-last-ids/ Share on other sites More sharing options...
Mark Baker Posted March 27, 2009 Share Posted March 27, 2009 SELECT MAX(id) as MAXID, MIN(id) AS MINID FROM content Link to comment https://forums.phpfreaks.com/topic/151376-solved-getting-first-and-last-ids/#findComment-795075 Share on other sites More sharing options...
ober Posted March 27, 2009 Share Posted March 27, 2009 I can only imagine why you would want to do that. How about you tell us what you're trying to do and we can give you a better suggestion? Grabbing the IDs isn't hard, but I'd like to know why you want to do it. Link to comment https://forums.phpfreaks.com/topic/151376-solved-getting-first-and-last-ids/#findComment-795078 Share on other sites More sharing options...
Canman2005 Posted March 27, 2009 Author Share Posted March 27, 2009 Well the rows returned are in ORDER of ID, so I just want to print the word "first row" and the word "last row" next to the relevant one. Such as if($row['id'] == $firstrowid) { print "first row"; } Does that make sense? Link to comment https://forums.phpfreaks.com/topic/151376-solved-getting-first-and-last-ids/#findComment-795097 Share on other sites More sharing options...
lonewolf217 Posted March 27, 2009 Share Posted March 27, 2009 just use $count = mysql_num_rows($show); if($row['id']==$count) { echo "last row"; } Link to comment https://forums.phpfreaks.com/topic/151376-solved-getting-first-and-last-ids/#findComment-795106 Share on other sites More sharing options...
Canman2005 Posted March 27, 2009 Author Share Posted March 27, 2009 Managed to sort it with $maxmindata = mysql_query("SELECT MIN(id) AS idfirst, MAX(id) AS idlast FROM websitecontent"); $maxminrow = mysql_fetch_array($maxmindata); echo $maxminrow[idfirst]; echo "<br>"; echo $maxminrow[idlast]; Thanks guys Link to comment https://forums.phpfreaks.com/topic/151376-solved-getting-first-and-last-ids/#findComment-795112 Share on other sites More sharing options...
Yesideez Posted March 27, 2009 Share Posted March 27, 2009 just use $count = mysql_num_rows($show); if($row['id']==$count) { echo "last row"; } That wouldn't work if some rows had been deleted. Link to comment https://forums.phpfreaks.com/topic/151376-solved-getting-first-and-last-ids/#findComment-795135 Share on other sites More sharing options...
lonewolf217 Posted March 27, 2009 Share Posted March 27, 2009 why not ? Deleted rows are removed from the table so it would still return the count of active rows. then I am not echoing the ID of the last row as a "counter", I am just saying that this is the last entry so echo "last row" Link to comment https://forums.phpfreaks.com/topic/151376-solved-getting-first-and-last-ids/#findComment-795136 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.