Here's how I've set up
class Core
{
public $dbh;
private static $instance;
private function __construct()
{
$dsn = 'mysql:host=' . CoreConfig::read('db.host') .
';dbname=' . CoreConfig::read('db.basename') .
';port=' . CoreConfig::read('db.port') .
';connect_timeout=15';
$user = CoreConfig::read('db.user');
$password = CoreConfig::read('db.password');
$this->dbh = new PDO($dsn, $user, $password);
}
public static function getInstance()
{
if (!isset(self::$instance))
{
$object = __CLASS__;
self::$instance = new $object;
}
return self::$instance;
}
}
class CoreConfig
{
static $confArray;
public static function read($name)
{
return self::$confArray[$name];
}
public static function write($name, $value)
{
self::$confArray[$name] = $value;
}
}
Include File: db.php
CoreConfig::write('db.host', SERVER);
CoreConfig::write('db.port', '3306');
CoreConfig::write('db.basename', DB);
CoreConfig::write('db.user', USER);
CoreConfig::write('db.password', PASSWORD);
Now I'm trying to convert my site from using MySQL to PDO. However, after reviewing some of my queries and page loading time, I've found that my queries are a lot slower. Here's a simplified example of how I used to use MySQL in one of my classes:
MySQL Version
class lotInfo
{
protected $_lot;
public function __construct($lot)
{
$this->_lot = $lot;
}
public function numJobsMil()
{
$JOB_IDS = array();
$SQL = mysql_query("SELECT job_id FROM job_materials_lot WHERE lot='".$this->_lot."'") or die(mysql_error());
while($data=mysql_fetch_array($SQL))
{
array_push($JOB_IDS,$data['job_id']);
}
$SQL2 = mysql_query("SELECT job_id FROM job_materials_prop WHERE lot='".$this->_lot."'") or die(mysql_error());
while($data2=mysql_fetch_array($SQL))
{
array_push($JOB_IDS,$data2['job_id']);
}
return $JOB_IDS;
}
}
When calling the method from within a loop on my page, the data loads in 0.6 seconds. For example:
require_once 'db.php'
$lots = array("SIB","ABC","DEF","ZSP"); // etc etc etc
foreach($lots as $x => $lot)
{
$info = new lotInfo($lot);
$jobs[] = $info->numJobsMil();
}
// do stuff
Now, the PDO version of my class as follows:
PDO Version:
class lotInfo
{
protected $_lot;
protected $_pdo;
public function __construct($lot)
{
$core = Core::getInstance();
$this->_pdo = $core;
$this->_lot = $lot;
}
public function numJobsMil()
{
$JOB_IDS = array();
$STH = $this->_pdo->dbh->query("SELECT job_id FROM job_materials_lot WHERE lot='".$this->_lot."'");
$STH->setFetchMode(PDO::FETCH_ASSOC);
$SQL = mysql_query("SELECT job_id FROM job_materials_lot WHERE lot='".$this->_lot."'") or die(mysql_error());
while($data = $STH->fetch())
{
array_push($JOB_IDS,$data['job_id']);
}
$STH_sub = $this->_pdo->dbh->query("SELECT job_id FROM job_materials_prop WHERE lot='".$this->_lot."'");
while($sub_data = $STH_sub->fetch())
{
array_push($JOB_IDS,$sub_data['job_id']);
}
return $JOB_IDS;
}
}
So far, this works perfectly, however when running this through the same loop, the data loads on average of 3.6 seconds whenever a user accesses the page. Now, I'm not sure why this is happening, but I'd like to get some opinions on this.