指定foreach" as"变量作为关联数组

时间:2015-05-20 00:28:16

标签: php arrays mysqli foreach

我为Cronjobs编写了这个小脚本,以便在他们的年度子设备启动时使用户失效。我用第一个查询($ results)拉出一个关联数组,然后绑定到foreach循环的$ id,但它不会运行,因为它没有选择在为$ id指定的索引上。如何指定$ id是一个关联数组,所以当$ results变为$ id时,我的索引是相同的?

由于

继承我的代码

<?php
header('Content-Type: text/plain');
include_once '../php/link_auth.php';
date_default_timezone_set('America/New_York');
$q = 'SELECT id, username, firstname, lastname, email, elevation FROM `members` WHERE (DATEDIFF(curdate(), members.regdate) > 365)';
$results = $mysqli -> query($q);
$i = 0;
$expired_users = "";
foreach($results->fetch_assoc() as $id){
    if($id['elevation'] != 'admin') {
    $user = $id['id'];
    $qUnsub = "UPDATE `members` SET `payment_status` = 'unsub' WHERE id = '$user'";
    $mysqli -> query($qUnsub);
    $i++;
    $expired_users .= PHP_EOL.'ID: '.$id['id'].PHP_EOL.'Username: '.$id['username'].PHP_EOL.'Name: '.$id['firstname'].' '.$id['lastname'].PHP_EOL.'Email Address: '.$id['email'].PHP_EOL;
    }
}
echo 'Report for expired subscriptions on '.date('l jS \of F Y h:i:s A').PHP_EOL.'Total users unsubbed today: '.$i.PHP_EOL.'Information related to expired accounts:'.PHP_EOL.$expired_users;
exit();

0 个答案:

没有答案