hoopplaya4 Posted March 25, 2009 Share Posted March 25, 2009 How would I go about removing the last comma in the following code: <?php $sql = "SELECT * FROM tblUsers"; require("../connection.php"); $rs=mysql_db_query($DBname,$sql,$link) or die(mysql_error()); while($row = mysql_fetch_assoc($rs)) { $users = $row['usrID']; echo $users; echo ","; } It outputs: 1,4,6,8,9, However, I need to remove the last comma, and I'm not sure how to do this within a "while" statement. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/151011-solved-quick-question-remove-comma/ Share on other sites More sharing options...
sasa Posted March 25, 2009 Share Posted March 25, 2009 <?php $comma = ''; $sql = "SELECT * FROM tblUsers"; require("../connection.php"); $rs=mysql_db_query($DBname,$sql,$link) or die(mysql_error()); while($row = mysql_fetch_assoc($rs)) { $users = $row['usrID']; echo $comma,$users; $comma = ","; } Quote Link to comment https://forums.phpfreaks.com/topic/151011-solved-quick-question-remove-comma/#findComment-793350 Share on other sites More sharing options...
Yesideez Posted March 25, 2009 Share Posted March 25, 2009 $query=mysql_query("SELECT * FROM tblUsers"); $numrecords=musql_num_rows($query); while ($row=mysql_fetch_assoc($query)) { echo $row['usrID'].($numrecords-->0 ? ', ' : ''); } Quote Link to comment https://forums.phpfreaks.com/topic/151011-solved-quick-question-remove-comma/#findComment-793403 Share on other sites More sharing options...
taquitosensei Posted March 25, 2009 Share Posted March 25, 2009 I would do it this way <?php $sql = "SELECT * FROM tblUsers"; require("../connection.php"); $rs=mysql_db_query($DBname,$sql,$link) or die(mysql_error()); while($row = mysql_fetch_assoc($rs)) { $users[] = $row['usrID']; } echo implode(",", $users); Quote Link to comment https://forums.phpfreaks.com/topic/151011-solved-quick-question-remove-comma/#findComment-793506 Share on other sites More sharing options...
hoopplaya4 Posted March 25, 2009 Author Share Posted March 25, 2009 Thanks all! Last method worked perfect for me. Quote Link to comment https://forums.phpfreaks.com/topic/151011-solved-quick-question-remove-comma/#findComment-793634 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.