我有一个mysql表,我保留了电子邮件地址。结构如下:
id || email
但是,我如何在php中实际输出用逗号分隔的电子邮件地址?
提前感谢您的帮助!
答案 0 :(得分:2)
使用:
<?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);
?>
参考文献:
答案 1 :(得分:0)
Google第三次点击“php mysql”:http://www.freewebmasterhelp.com/tutorials/phpmysql
你可能需要阅读基础知识,这并不复杂,只需几分钟就可以浏览它并让你开始。
答案 2 :(得分:0)
选项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;
选项2,以便您稍后可以对所有电子邮件执行更多操作:
$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) {
// ...
}
答案 3 :(得分:0)
做类似的事情:
while ($output = mysql_fetch_array($query_results)) {
echo $output['id'] . ", " . output['email'];
}