1

I have a mysql table in which I keep e-mail addresses. The structure is as follows:

id    ||    email

However, how do I actually output those e-mail addresses, separated by commas, in php?

Thanks in advance for your help!

4 Answers 4

2

Use:

<?php
 $query = "SELECT GROUP_CONCAT(t.email) AS emails
             FROM YOUR_TABLE t"

 // Perform Query
 $result = mysql_query($query);

 while ($row = mysql_fetch_assoc($result)) {
    echo $row['emails'];
 }

 // Free the resources associated with the result set
 // This is done automatically at the end of the script
 mysql_free_result($result);
?>

References:

Sign up to request clarification or add additional context in comments.

Comments

0

Third hit on Google for "php mysql": http://www.freewebmasterhelp.com/tutorials/phpmysql

You probably need to read up on the basics, it's not complicated and will only take a few minutes to glance over it and get you started.

Comments

0

Option 1:

$sql = "SELECT GROUP_CONCAT(email ORDER BY email ASC SEPARATOR ', ') AS emails FROM emails";
$res = mysql_query($sql);
list($emails) = mysql_fetch_row($res);

echo $emails;

Option 2, so you can do more with all the emails later on:

$emails = array();

$sql = "SELECT email FROM emails ORDER BY email ASC";
$res = mysql_query($sql);
while ($r = mysql_fetch_object($res)) {
  $emails[] = $r->email;
}

// as a list
echo implode(', ',$emails);

// or do something else
foreach ($emails as $email) {
  // ...
}

Comments

0

Do something like:

while ($output = mysql_fetch_array($query_results)) {
  echo $output['id'] . ", " . output['email'];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.