mysql php pdo 迭代器,创建PDO迭代器

I'm flowing this article to create Database Iterator. I've similar tables and records. But when I executing I'm getting blank page. I think issue on $data = new DbRowIterator($stmt);, It's not returning proper iterator to run accept method in FilterIterator class. I'm using laravel

解决方案

This article is a hoax. It claims that offered technique would speed up database calls with PDO, but in fact it slows them down significantly.

Premises on which it is grounded are also wrong. PDOStatement is already traversable, you don't need no tricks to iterate over PDOStatement using foreach.

The benchmarking section (as it often happens) is a blatant swindle. The guy is comparing fetchAll(), which obviously consumes a lot of time/memory, with fetching a single row at a time. And even this way his timing is much worse than with a proper solution.

The guy who wrote this article knows no PDO and - worse yet - no SQL.

Everything he invented already exists in PDO and SQL:

PDOStatement is already traversable. No need to reinvent the wheel.

The most important part: filtering has to be done in SQL, not in PHP. That's a textbook rule. If you need only 63992 out of 250000 records, you should select only 63992 in the first place. And if you ask a database to select the records for you, it will be incomparable faster.

So, to get everything this guy wrote with such effort, you need only few lines with PDO

$period = date_create("last_week")->format('Y-m-d 00:00:00');

$sql = 'SELECT * FROM `gen_contact` WHERE contact_modified > ? ORDER BY `contact_modified` DESC';

$stmt = $pdo->prepare($sql);

$stmt->execute([$period]);

foreach ($stmt as $row) {

echo sprintf(

'%s (%s)| modified %s',

$row->contact_name,

$row->contact_email,

$row->contact_modified

) . PHP_EOL;

}

And this code will be a multitude times faster as it does the filtering on the database level, instead of fetching the whole table and then filtering on PHP side.

As of the error you get, just set PDO in exception mode and watch for the regular PHP errors.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值