cyborgzombieninjapirate


Apples and Oranges. MySQLi and PDO.

Posted on 12.07.2009 04:07 pm

Every blog system needs a good database connection. I had over the years used the classic MySQL extension and had poked around with MySQLi a little bit.

But PDO had piqued my interest. It's not a full blown database abstraction but it had data-access abstraction. And it used OO better than MySQLi.

I wrote a few small functions and abstracted away a few nuances of PDO I didn't like. For example I wanted all results as objects. And PDO supports that if you supply PDO::FETCH_OBJ in each fetch request.

Here's the first version of the fetch() function I am using. It has a few bugs and no error reporting but I will come to those items.

/**
 * Used to fetch data from the database.
 * First argument is the SQL. Then the values.
 * 
 * Example fetching blog titles:
 * function getBlogTitle()
 * {
 *     return $this->fetch("SELECT `title` FROM `blog` WHERE `id` = ?", $id);
 * }
 * 
 * This will replace ? with $id
 */
function fetch()
{
	/**
	 * Dynamic arguments
	 */
	$args = func_get_args();
	$sql = array_shift($args);

	/**
	 * PDO setup
	 */
	$prepare = $this->db->prepare($sql);
	$prepare->execute($args);

	/**
	 * Return all data as objects
	 */
	return $prepare->fetchAll(PDO::FETCH_OBJ);
}

But what worried my was the speed of PDO. So I branched the blog and rewrote the database layer in MySQLi and profiled it with the PDO version

(check the links for pretty and scientific images).

And what I saw was minimal changes. MySQLi was a bit quicker to connect (3% faster) but a MySQLi fetch was slower than a PDO fetch (1.5% slower). These are not numbers you gasp in awe at and decide that you should switch.

These are numbers you look at and say "This isn't a significant result". I ran the profiler a few times. The numbers jumped up and down by about 2-4%. So there is no say in this at the moment. And I will stick to PDO for now.

0 6    Like it or hate it?  -  Comment (0)


(Used for gravatar only, never displayed)

What is 9 + 5


Memory allocated for your request: 181.47 Kb
Process time: 0.00877 seconds