随机"非法抵消"错误

时间:2015-05-04 19:10:41

标签: php mysql email loops while-loop

以下代码每次while()循环时都会产生非法偏移错误:

  

警告:非法字符串偏移'用户名'在第48行的/Applications/MAMP/htdocs/admin/scripts/email_owners_send.php中

如果$name 不是空的,并且是<的话,我试图将$email['username']设为$email['username'] / strong>为空,$name应设置为$email$emailwhile()语句设置mysql_fetch_assoc()。这是我目前的代码:

// print out all the email address recipients
$query = mysql_query("SELECT * FROM ownersemails WHERE deleted=0 LIMIT 5");
$recipients = '';
$total = mysql_num_rows($query);
$num = 0;
while($email = mysql_fetch_assoc($query)) {
    // add to count
    $num++;

    // set email
    $email = $email['email'];

    // set name as username if apparant
    if(!empty($email['username'])) {
        $name = $email;
    }
    else {
        $name = $email['username'];
    }

    // add to recipients string (=. append)
    $recipients .= '{ "email": "'.$email.'", "name": "'.$name.'" }';
    // only add a comma if it isn't the last one
    if($num!=$total) {
        $recipients .=',';
    }
}

1 个答案:

答案 0 :(得分:1)

您有一个名为$email的数组,它包含数据库行中的所有列。并且您只使用电子邮件列的值覆盖该对象:

while($email = mysql_fetch_assoc($query)) {
    // add to count
    $num++;

    // set email
    $email = $email['email'];
...

不要那样做。调用行对象$row会更有意义。您应该将变量命名为明显的变量。

while($row = mysql_fetch_assoc($query)) {
    // add to count
    $num++;

    // set email
    $email = $row['email'];
...