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 Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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"; } Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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" Quote Link to comment 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.