Kay1021 Posted May 14, 2009 Share Posted May 14, 2009 I was wondering if there was a way to tell the last result from an sql statement. Or maybe there is an easier way all together. Basically i have categories being outputted based on the sql statement. However after each one I wanted a comma....the problem with the code I have now (see below)...is that it also adds a comma to the very last one...that looks odd. I know mysql_num_rows tells you the total number of results....so i figured you could use that along with a counter....but i can't seem to work out the right code...either the comma remains or a i get weird results.... Any advice? Thanks $sql2="SELECT * FROM category WHERE cat_name = '$curr_cat'"; $qry3 = mysql_query($sql2); $total = mysql_num_rows($qry3); if (mysql_num_rows($qry3) > 0) { while ($rs = mysql_fetch_assoc($qry3)) { $category= $rs['area']; print $category; if($total >1){ print ', '; } } } the output right now for one results is Chocolate for more than one result Chocolate, Candy, Chips, Pop, for more than one result I would like it to be: Chocolate, Candy, Chips, Pop Quote Link to comment https://forums.phpfreaks.com/topic/158194-determine-last-result-of-sql-statement/ Share on other sites More sharing options...
Ken2k7 Posted May 14, 2009 Share Posted May 14, 2009 You don't have to. Let MySQL do it. It'll be easier on your part too! $sql2 = "SELECT GROUP_CONCAT(area) AS listings FROM category WHERE cat_name = '$curr_cat' LIMIT 1"; $result = mysql_query($sql2) or die(mysql_error()); $result = mysql_fetch_assoc($result) or die(mysql_error()); echo $result['listings']; Like it? Well I guess it's not exactly like yours. MySQL doesn't add the space after the comma. But you can use substr if you really want to use your method. Quote Link to comment https://forums.phpfreaks.com/topic/158194-determine-last-result-of-sql-statement/#findComment-834432 Share on other sites More sharing options...
Kay1021 Posted May 14, 2009 Author Share Posted May 14, 2009 Thank you thank you! Way easier....i've never seen that stuff before....but it looks like it's good to know! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/158194-determine-last-result-of-sql-statement/#findComment-834433 Share on other sites More sharing options...
JD* Posted May 15, 2009 Share Posted May 15, 2009 You can also do the following: $result = mysql_query("SELECT * FROM category WHERE cat_name = '$curr_cat'") or die(mysql_error()); for($i=0;$i < mysql_num_rows($result);$i++) { echo mysql_result($result, $i, "area").($i + 1 < mysql_num_rows($result) ? ", " : ""); } If I did it right, it should give you each area followed by a comma until it reaches the last one, where it will not. Quote Link to comment https://forums.phpfreaks.com/topic/158194-determine-last-result-of-sql-statement/#findComment-834498 Share on other sites More sharing options...
Kay1021 Posted May 15, 2009 Author Share Posted May 15, 2009 Thank you that is also handy...i couldn't for the life of me remember how to do the loop Quote Link to comment https://forums.phpfreaks.com/topic/158194-determine-last-result-of-sql-statement/#findComment-834513 Share on other sites More sharing options...
Ken2k7 Posted May 15, 2009 Share Posted May 15, 2009 JD*, you realize that you're making a lot of function calls to mysql_num_rows(), right? If that SQL were to return 100 rows, you'll be executing that function 200 times. And each time, it has to traverse an array of 100 elements. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/158194-determine-last-result-of-sql-statement/#findComment-834538 Share on other sites More sharing options...
Psycho Posted May 15, 2009 Share Posted May 15, 2009 Ken2k7's option is probably the most efficient, but just for the sake of learning here's another approach <?php $query = "SELECT * FROM category WHERE cat_name = '$curr_cat'"; $result = mysql_query($query) or die(mysql_error()); while($record = mysql_fetch_assoc($result)) { $catNames = $record['area']; } echo implode(', ', $catNames); ?> Quote Link to comment https://forums.phpfreaks.com/topic/158194-determine-last-result-of-sql-statement/#findComment-834565 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.