Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating PDO Iterator

Tags:

php

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

like image 670
KKK Avatar asked Sep 09 '16 11:09

KKK


People also ask

How do you write PDO?

$pdo = new PDO($dsn, $user, $passwd); A new PDO object is created. We pass the constructor the data source name and the user name and password. The PDO class represents a connection between PHP and a database server.

Is PDO an ORM?

PDO and ORM are two entirely different things. PDO is a specific implementation of a Database Access Abstraction Layer, it enables you to connect, run SQL and retrieve results from the database with an API that is consistent across different database backends (e.g. MySQL, PostgreSQL, MS SQL, etc.)

Which is better MySQLi or PDO?

Both MySQLi and PDO have their advantages: PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases. So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries.

What is PDO in SQL?

PDO (PHP Data Objects) is an abstraction layer for your database queries and is an awesome alternative to MySQLi, as it supports 12 different database drivers. This is an immense benefit for people and companies that need it. However, keep in mind that MySQL is by far the most popular database.


1 Answers

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:

  1. PDOStatement is already traversable. No need to reinvent the wheel.
  2. 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.

like image 112
Your Common Sense Avatar answered Oct 01 '22 14:10

Your Common Sense