<?php /*¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯*\ | Abstract PDO Singleton | | Writen by Booster2ooo for HWC-Crew | | ... | \*___________________________________________________*/ //Abstract Singleton for PDO MySQL class Engine2 { //$sqlCon is our DB unique instance private static $sqlCon; //As Engine is abstract, its contructor can only be called by a child protected function __construct($host, $db, $user, $pass) { //First, we are going to check if the instance $sqlCon of the DB exists. If it doesn't we create one if(!isset(self::$sqlCon) || self::$sqlCon == null) { if(empty($host)) $host = 'localhost'; if(empty($db)) $db = 'trends'; if(empty($user)) $user = 'root'; try { //As it's the first time we call Engine2, we create a new(and unique) DB connection self::$sqlCon = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass); //self::$sqlCon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); } catch(PDOException $e) { echo '<p class="error">'; echo 'PDO Error : '.$e->getMessage().'<br />'; echo 'N° : '.$e->getCode(); echo '</p>'; } } } //This method provides the mecanism for creating and executing SQL Queries from a child using the unique $sqlCon DB connection instance // - $query represents the SQL query that's going to be executed // - $params is an optinal parameter allowing you to pass the data to bind to the prepared request protected function sqlQuery($query, $params=array('')) { try { $prep_req = self::$sqlCon->prepare($query); $prep_req->execute($params); $reply = $prep_req->fetchAll(PDO::FETCH_OBJ); $prep_req->closeCursor(); return $reply; } catch(PDOException $e) { echo '<p class="error">'; echo 'Error : '.$e->getMessage().'<br />'; echo 'N° : '.$e->getCode().'<br />'; echo 'Request: '.$query.'<br />'; echo 'Parameters: <br />'; var_dump($params); echo '</p>'; } } } ?> <?php //Sample Class inheriting from Engine2 class Sample extends Engine2 { //The constructor needs to know the SQL Hostserver, SQL DB, SQL User and SQL Pass (can be null) public function __construct($host, $db, $user, $pass) { try { //We call the parent (Engine2) constructor with the same parameters parent::__construct($host, $db, $user, $pass); //We check if the table exists $query = "SHOW TABLES LIKE ?"; $params = array('Engine2_Sample'); $reply = $this->sqlQuery($query, $params); //If it doesn't, we create it if (empty($reply)) $this->install(); } catch(PDOException $e) { echo '<p class="error">'; echo 'PDO Error : '.$e->getMessage().'<br />'; echo 'N° : '.$e->getCode(); echo '</p>'; } } //Install the table private function install() { $query = "CREATE TABLE IF NOT EXISTS `Engine2_Sample` (" ."`id` int(11) NOT NULL AUTO_INCREMENT," ."`text` text NOT NULL," ."PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1"; $this->sqlQuery($query); } //Check the db for a text entry and add it to the DB if it doesn't exists public function add($text) { if(!empty($text)) { $query = "SELECT `text` FROM `Engine2_Sample` WHERE `text` = ?"; $params = array($text); $result = $this->sqlQuery($query, $params); if(empty($result)) { $query = "INSERT INTO `Engine2_Sample` VALUES('',?)"; $params = array($text); $this->sqlQuery($query, $params); return true; } else { return false; } } } } } ?>
Booster2ooo