dongliping003116 2014-04-02 14:47
浏览 42
已采纳

无法从我的数组创建逗号分隔列表

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! :)

  • 写回答

2条回答 默认 最新

  • donglv5269 2014-04-02 14:50
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料