Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

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 -

Name1,Name2,Name3,Name4,Name5

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

Thank you!
0
SevenAteAnthony
Asked:
SevenAteAnthony
  • 2
  • 2
1 Solution
 
PragmatiCoderCommented:
Try this:

<?php 
    $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

0
 
SevenAteAnthonyAuthor Commented:
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!
0
 
PragmatiCoderCommented:
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[] = $getAllEmailsInQueue['Email'];//You typed $row
}
$concatEmails = implode(',',$emails);
$resultSend = "<p>".$concatEmails."</p> - Test";
} else {
$resultSend = "<p>Error: You have not selected anyone.</p>";
}

Open in new window

0
 
SevenAteAnthonyAuthor Commented:
Thanks so much! It's getting late...
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now