0

I am using the following code

while($result1=mysql_fetch_assoc($sql))
{

    $result=$result1['userId'];
    $arr=explode(" ",$result);
    $userId=implode(",",$arr);
    echo $userId;
}

But I am getting the output like this:5354 I want my output 53,54 please help me

A.C.Manikandan
  • 227
  • 2
  • 3
  • 12
  • [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). – Rizier123 Apr 04 '16 at 04:34

1 Answers1

2

You first want to collect your values, which you fetch from your db, into an array. Then you want to implode() it.

while($result1=mysql_fetch_assoc($sql)){   
    $result[] = $result1['userId'];
}

echo implode(",",$result);

Right now in your code you simply explode one value into an array, but it can't split the string on the delimiter, so it just puts your value into the first array element.

Then imploding an array with only 1 element doesn't make much sense and you simply just output the value again in the while loop. So what your current code does right now is just:

while($result1=mysql_fetch_assoc($sql)){
    echo $result1['userId'];
}
Rizier123
  • 58,877
  • 16
  • 101
  • 156