2010-10-11 141 views
1

我有一个MySQL表,其中我保留电子邮件地址。结构如下:在php中如何显示mysql结果?

id || email 

但是,如何实际输出这些电子邮件地址,用逗号分隔,在PHP中?

在此先感谢您的帮助!

回答

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); 
?> 

参考文献:

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) { 
    // ... 
}
0

做这样的事情:

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