例如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();
}我认为在循环之后做同样的事情并不酷。
所以我想减少1号街区。
如果使用fetchAll()和foreach,那么在循环中判断最后一个是很容易的。但是,将大量的结果数据转换为数组和保存正在增加php内存。
有谁知道酷的路吗?
抱歉,英语坏了。
发布于 2014-04-24 14:24:16
试试这个,也许它能帮你:-
$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;
}
}发布于 2014-04-24 14:27:19
使用$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;
}
}https://stackoverflow.com/questions/23271514
复制相似问题