e.g。 iOS推送程序
$push = new ApnsPHP();//ApnsPHP push class
/*
...
*/
$limit = 1000;
//want not to use fetchAll() method so as to reduce php memory.
$stmt = $pdo->query('SELECT * FROM table');
$i = 0;
while ($row = $stmt->fetch()) {
$send = true;
++$i;
//adding a que by max $limit count
$push->add($row['token'],$row['msg']);
if($i % $limit === 0){
$push->send();
$send = false;
}
}
//send rest que if remaining :#1
if($send){
$push->send();
}
我觉得在while循环之后做同样的事情并不酷。
所以我想减少#1块。
如果使用fetchAll()和foreach,在循环中判断最后一个很容易。 但是将所有结果数据转换为数组并保持正在增加php内存。
有谁知道很酷的方式?
抱歉英语不好。
答案 0 :(得分:0)
试试这个,可能对你有帮助: -
$stmt = $pdo->query('SELECT * FROM table');
$total_count = $stmt->rowCount();
$i = 0;
while ($row = $stmt->fetch())
{
$send = true;
++$i;
if($total_count == $i)
{
//Do Process For Last Record
}
//adding a que by max $limit count
$push->add($row['token'],$row['msg']);
if($i % $limit === 0){
$push->send();
$send = false;
}
}
答案 1 :(得分:0)
使用$stmt->rowCount()
计算选定的行数。
$stmt = $pdo->query('SELECT * FROM table');
$i = 0;
$numRows = $stmt->rowCount();
while ($row = $stmt->fetch()) {
$send = true;
++$i;
if($numRows == $i){
echo 'Last iteration';
}
//adding a que by max $limit count
$push->add($row['token'],$row['msg']);
if($i % $limit === 0){
$push->send();
$send = false;
}
}