Jump to content

[SOLVED] Quick Question, Remove Comma


hoopplaya4

Recommended Posts

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

<?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 = ","; }

 

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); 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.