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. 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 = ","; } 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 ? ', ' : ''); } 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); 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. 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
Archived
This topic is now archived and is closed to further replies.