Avatar of SevenAteAnthony
Flag for United States of America asked on

Query Names From Database, Display All With Commas After Each Except Last One

Hi Experts,

I am looking for a quick code segment where I can take a bunch of names I've queried from a table on my database, and organize them like this -


Note that the last one does not have a comma. Can anyone help me? I could never understand a for statement.

Thank you!
PHPMySQL ServerDatabases

Avatar of undefined
Last Comment

8/22/2022 - Mon

Try this:

    $query = "SELECT names FROM table WHERE 1";
    $result = mysql_query($query);

    while($row = mysql_fetch_array($result)){
        $names[] = $row['names'];
    $concatNames = implode(',',$names)

    echo $concatNames;

Open in new window


Thank you for your post, PragmatiCoder! My code below does not seem to output the $concatEmails field, though...

if (isset($_POST['sendEmail'])) {
$resumeUser = $_SESSION['user_id'];
$getAllEmailsInQueueQuery = mysql_query("SELECT Email FROM SearchQueue WHERE ResumeUser='$resumeUser'");
$numrows = mysql_num_rows($getAllEmailsInQueueQuery);
if ($numrows > 0) {
while ($getAllEmailsInQueue = mysql_fetch_array($getAllEmailsInQueueQuery)) {
$emails[] = $row['Email'];
$concatEmails = implode(',',$emails);
$resultSend = "<p>".$concatEmails."</p> - Test";
} else {
$resultSend = "<p>Error: You have not selected anyone.</p>";

Open in new window

Can you please help me get it to output them?

Thank you!

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Thanks so much! It's getting late...
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes