duanli8391 2013-10-10 08:32
浏览 71
已采纳

致命错误:for循环中PHP数组上的字符串错误不支持[]运算符

I have this PHP Code:

for($i=1; $i<=$_POST["counter"]; $i++)
{
    if($_POST["checkbox$i"])
    {
        //select the billing_pdf row
        $sql="SELECT * from billing_pdf_archive where sequence = '".$_POST["checkbox$i"]."' ";
        $rs=mysql_query($sql,$conn);
        while($result=mysql_fetch_array($rs))
        {
            //now select all contacts for the company who receive all accounts emails
            $sql2="SELECT * from contacts where company_sequence = '".$result["customer_sequence"]."' and receive_accountsemails = 'yes' ";
            $rs2=mysql_query($sql2,$conn) or die(mysql_error());
            if(mysql_num_rows($rs2) > 0)
            {
                while($contacts2=mysql_fetch_array($rs2))
                {
                    //generate the list of emails address in an array
                    $emails_list[] = $contacts2["email"];
                }
                $emails_list = implode(',',$emails_list);
            }
        }

        $ins_sql2="INSERT into email_attachments (email_seq, attachment) values ('".$email_sequence."', '".$result["pdf"]."') ";
        $ins_rs2=mysql_query($ins_sql2,$conn) or die(mysql_error());

        $up_sql="UPDATE emails set emailto = '".$emails_list."' where sequence = '".$email_sequence."' ";
        $up_rs=mysql_query($up_sql,$conn);
    }
}

but I am getting this error:

Fatal error: [] operator not supported for strings in

on the line that says:

$emails_list[] = $contacts2["email"];

I am using the same array code on other pages (without the for loop) and they work fine

what am I doing wrong?

  • 写回答

2条回答 默认 最新

  • doujiu5464 2013-10-10 08:39
    关注

    you fill the array $emails_list[] in the second while with $contacts2["email"]. in the next line, you implode the array to a string.

    for the next result (first while loop) $emails_list is a string. now you can't convert a string to an array

    try this:

            $emailsListData = array();
            while($contacts2=mysql_fetch_array($rs2))
            {
                //generate the list of emails address in an array
                $emailsListData[] = $contacts2["email"];
            }
            $emails_list = implode(',',$emailsListData);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝