【发布时间】:2015-07-14 07:24:57
【问题描述】:
以下代码在每次while() 循环时都会产生非法偏移错误:
警告:第 48 行 /Applications/MAMP/htdocs/admin/scripts/email_owners_send.php 中的非法字符串偏移“用户名”
如果$email['username'] 不为空,我正在尝试将$name 设置为$email['username'],如果为为空,则$name 应该为设置为$email。 $email 由while() 语句设置,来自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 .=',';
}
}
【问题讨论】:
-
请stop using
mysql_*functions。它们不再被维护并且是officially deprecated。改为了解prepared statements,并考虑使用PDO。
标签: php mysql email loops while-loop