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

时间:2013-10-10 08:32:48

标签: php

我有这个PHP代码:

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

但是我收到了这个错误:

  

致命错误:

中的字符串不支持[]运算符

在线上说:

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

我在其他页面上使用相同的数组代码(没有for循环),它们工作正常

我做错了什么?

2 个答案:

答案 0 :(得分:2)

使用$emails_list[]填充第二个数组$contacts2["email"]。 在下一行中,您将数组内爆为字符串。

表示下一个结果(while while循环)$emails_list是一个字符串。现在你无法将字符串转换为数组

试试这个:

        $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 :(得分:2)

您已经将$ emails_list用作字符串中某些位置的字符串 $ emails_list = 7;

再次使用它

$ emails_list [] = array(1,2,4);