Skip to content

Instantly share code, notes, and snippets.

@mbernson
Created June 14, 2012 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbernson/2929492 to your computer and use it in GitHub Desktop.
Save mbernson/2929492 to your computer and use it in GitHub Desktop.
PDO versus ext/mysql test
CREATE DATABASE `php_mysqltest`;
CREATE TABLE `test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`test_int` int(11) DEFAULT NULL,
`test_float` float DEFAULT NULL,
`test_string` varchar(40) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
<?php
$startTime = microtime(true);
$dbh = new PDO('mysql:dbname=php_mysqltest;host=localhost', 'php', 'php');
for($i = 0; $i < 90000; $i++) {
$st = $dbh->prepare('INSERT INTO test (`test_int`, `test_float`, `test_string`) VALUES (:int, :float, :string)');
$st->bindValue(':int', mt_rand(), PDO::PARAM_INT);
$st->bindValue(':float', mt_rand()/mt_rand()*10);
$st->bindValue(':string', sha1(mt_rand()));
$st->execute();
unset($st);
}
$endTime = microtime(true);
echo 'Inserting ', $i, ' rows took ', $endTime - $startTime, ' seconds';
<?php
$startTime = microtime(true);
mysql_connect('localhost', 'php', 'php');
mysql_select_db('php_mysqltest');
$q = mysql_query('SELECT * FROM test');
$result = array();
while($row = mysql_fetch_assoc($q)) {
$result[] = $row;
}
header('Content-Type: application/json');
echo json_encode($result);
$endTime = microtime(true);
echo 'Query took ', $endTime - $startTime, ' seconds';
<?php
$startTime = microtime(true);
$dbh = new PDO('mysql:dbname=php_mysqltest;host=localhost', 'php', 'php');
$result = $dbh->query('SELECT * FROM test')->fetchAll(PDO::FETCH_ASSOC);
header('Content-Type: application/json');
echo json_encode($result);
$endTime = microtime(true);
echo 'Query took ', $endTime - $startTime, ' seconds';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment