Try something like this:

$ids = array(); 
while ($row = mysql_fetch_assoc($result))  
{
    row["UserID"]; 
} 
echo implode(", ", $ids);

Replace "UserID" with the columnname of the id in your table.

So: first you build the array, next you implode the array into a string.

Answer from huysentruitw on Stack Overflow
🌐
PHP
php.net › manual › en › function.implode.php
PHP: implode - Manual
It's not obvious from the samples, if/how associative arrays are handled. The "implode" function acts on the array "values", disregarding any keys: <?php declare(strict_types=1); $a = array( 'one','two','three' ); $b = array( '1st' => 'four', 'five', '3rd' => 'six' ); echo implode( ',', $a ),'/', implode( ',', $b ); ?> outputs: one,two,three/four,five,six
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-create-comma-separated-list-from-array-in-php
How to Create Comma Separated List from an Array in PHP? - GeeksforGeeks
July 11, 2025 - <?php // Declare an array and initialize it $Array = array(0, 1, 2, 3, 4, 5, 6, 7); // Display the array elements print_r($Array); // Use implode() function to join // comma in the array $List = implode(', ', $Array); // Display the comma separated ...
🌐
sebhastian
sebhastian.com › php-array-to-comma-separated-string
PHP array to comma separated string (code snippets included) | sebhastian
September 20, 2022 - Once you have a single array returned from array_column(), call the implode() function. For example, suppose you want to create a comma separated string from the first_name data. Here’s how you do it: <?php // 👇 create multi dimension array ...
🌐
Agernic
agernic.com › php-tutorial › php-convert-array-to-string-comma-separated.html
PHP convert array to string comma separated, php tutorial
<!DOCTYPE html> <html> <head> <title>PHP convert array to string comma separated</title> </head> <body> <h1>PHP convert array to string comma separated</h1> <?php $arrays = array('first name','last name', 'town', 'email', 'phone'); $comma_separated = implode(", ", $arrays); echo $comma_separated ...
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-create-comma-separated-list-from-an-array-in-php
How to create comma separated list from an array in PHP?
December 26, 2019 - <?php $arr = array(" John ", "Jacob ", " Tom ", " Tim "); echo "Array with whitespaces:<br>"; foreach($arr as $value) { echo "Value = '$value'<br>"; } // Create comma-separated list with trimmed values $trimmed = array_map('trim', $arr); echo "\nComma separated list: "; echo implode(', ', $trimmed); ?>
🌐
freeCodeCamp
freecodecamp.org › news › php-implode-convert-array-to-string-with-join
PHP Implode – Convert Array to String with Join
October 6, 2022 - <?php $langs = array("PHP", "JavaScript", "Python", "C++", "Ruby"); $newLangs = implode($langs); // Since we are printing a string, we can use echo to display the output in the browser echo $newLangs; ?> Note that I did not pass in a separator and implode() still works fine. In the example below, I passed in an empty space, comma, and hyphen as separators: <?php $langs = array("PHP", "JavaScript", "Python", "C++", "Ruby"); $newLangsSpace = implode(" ", $langs); $newLangsComma = implode(", ", $langs); $newLangsHyphen = implode("-", $langs); // Since we are printing a string, we can use echo to display the output in the browser echo $newLangsSpace."<br>"."<br>"; echo $newLangsComma."<br>"."<br>"; echo $newLangsHyphen ."<br>"; ?> You can see it’s better to specify a separator so you can see the values well.
Find elsewhere
🌐
Pi My Life Up
pimylifeup.com › home › using the implode function in php
Using the implode function in PHP - Pi My Life Up
April 26, 2022 - Below we have shown the output from this PHP example. You can see how the implode function stripped out the array keys and joined each value together, using a comma as the separator
🌐
Ink Plant Code
inkplant.com › code › commas-and-and
Display List in PHP with Commas and "And"
When you have a list of items stored in an array, PHP’s built-in implode function does a decent job of quickly displaying them on the screen, but it fails to address the linguistic custom of adding an "and" before the last item in the list.
🌐
W3Schools
w3schools.com › php › func_string_implode.asp
PHP implode() Function
The implode() function returns a string from the elements of an array. Note: This function is binary-safe. ... <?php $arr = array('Hello','World!','Beautiful','Day!'); echo implode(" ",$arr)."<br>"; echo implode("+",$arr)."<br>"; echo implode("-",$arr)."<br>"; echo implode("X",$arr); ?> Try it Yourself »
🌐
Cyberscorpion
cyberscorpion.com › 2012-01 › using-phps-implode-function-to-display-an-array-as-a-value-separated-string
Using PHP’s implode() Function to Display an Array as a Value-Separated String — CyberScorpion Bytes
<?php print '<div>' . implode(', ', $movies_to_display) . '</div>'; ?> The first argument of the implode() function determines what's added between each array value. So what if we need to display the values as an HTML list?
🌐
SitePoint
sitepoint.com › php
Implode multidimensional array - PHP - SitePoint Forums | Web Development & Design Community
January 31, 2013 - Hi Guys I need to implode a multidimensional array. The require output is the value separated by commas. For example, I have the following array: Array ( [0] => Array ( [job_type] => Administrative / Clerical Jobs ) [1] => Array ( [job_type] ...
🌐
Phpmentoring
phpmentoring.org › blog › php-implode-function
PHP Implode() Function - Join Array Elements Into A String
In this final simple example, we create a multidimensional php array and use implode() to flatten the array structure into a single string with each element separated by a comma and space.
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php implode
A Basic Guide to PHP implode() function by Practical Examples
April 7, 2025 - If you pass an associative array to the implode() function, it will join the array’s values only and disregard the array keys. For example: <?php $person = [ 'first_name' => 'John', 'last_name' => 'Doe', 25, 'Female' ]; echo implode(',', $person);Code language: PHP (php) Try it · Output: John,Doe,25,FemaleCode language: PHP (php) The following defines a function called get_header() that returns a comma-separated list of columns: <?php function get_header(array $columns): string { if (1 === count($columns)) { return $columns[0]; } return implode(',', $columns); }Code language: PHP (php) The get_header() function returns the first element in the $columns array if the $columns array has one element.
🌐
Scientech Easy
scientecheasy.com › home › blog › php implode() function to join an array of strings
PHP implode() Function to Join an Array of Strings - Scientech Easy
May 29, 2025 - The basic syntax to define implode() function in PHP is as follows: ... $separator: This parameter is a string that will be used to separate the array elements in the resulting string. Common separators include commas (“,”), spaces (” “), dashes (“-“), and slashes (“/”).
🌐
Scaler
scaler.com › home › topics › php implode() function
PHP implode() Function - Scaler Topics
April 14, 2024 - Use the implode() function: Next, use the implode() function to convert the array into a string. The first parameter of the implode() function is the separator that you want to use between the array elements in the resulting string. In this example, the separator is a comma followed by a space.
🌐
Linux Hint
linuxhint.com › php-convert-array-comma-separated-string
Linux Hint – Linux Hint
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use