无法从我的数组创建逗号分隔列表
I'm a newbie in PHP. Here's my code:
$email_db = mysql_query("SELECT emails FROM email_feeds") or die(mysql_error());
$recipients = array();
while($row = mysql_fetch_assoc( $email_db )){
$recipients[] = $row;
}
$sendTo = implode(', ', $recipients);
echo($sendTo);
What I'm trying to do here is get the data from the column emails from the email_feeds table in my database, then put it in an array ($recipients) then echo it in a comma separated form.
E.g. john@mysite.com, mike@mysite.com, claire@mysite.com
My problem is when I run it it shows an "Array to string conversion" notice.
Please help! :)
我是PHP的新手。 这是我的代码: p>
$ email_db = mysql_query(“SELECT email from FROM email_feeds”)或die(mysql_error());
$ recipients = array();
while($ row = mysql_fetch_assoc($ email_db)){
$ recipients [] = $ row;
}
$ sendTo = implode(',',$ recipients);
echo($ sendTo);
code> pre>
我在这里要做的是从我的数据库中的 email_feeds strong>表中获取电子邮件 strong>列中的数据,然后将其放入 数组( $ recipients strong>)然后以逗号分隔的形式回显它。 p>
例如 john@mysite.com,myke@mysite.com,claire@mysite.com p>
我的问题是当我运行它时会显示“数组到字符串转换” strong >通知。 p>
请帮忙! :) p>
div>
mysql_fetch_assoc
returns an array of columns.
Even though you only have one column, it's still an array containing that one column.
So, when you write this:
$recipients[] = $row;
you're adding a new array to $recipients
, not a new string.
Imploding $recipients
tries to implode arrays as if they were strings, which they are not.
Try:
$recipients[] = $row['emails'];
Have a read of the documentation for the function you're using.
In particular, pay close attention to the big, red deprecation warning.
You are using mysql_fetch_assoc
, which returns an associative array. You really ought to switch to mysqli
functions. mysql_
functions are deprecated. When it returns an array, you are assigning an entire array to each element in the recipients array here:
$recipients[] = $row;
What you need is just the string from the array, so you can do this instead:
$recipients[] = $row['emails'];