Advertisement
Sk8erPeter

PDO DB Singleton class

Jul 20th, 2011
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.02 KB | None | 0 0
  1. <?php
  2.  
  3. // in the config file:
  4.  
  5. define('MYSQL_USER', 'mydb');
  6. define('MYSQL_PASS', 'myUser');
  7. define('MYSQL_DB',   'db_myDB');
  8.    
  9. define('PDO_CONNECTION', true);
  10.  
  11. define('PDO_MYSQL_DSN', 'mysql:dbname='.MYSQL_DB.';host=localhost;charset=UTF-8');
  12.  
  13. // Singleton
  14. class DB{
  15.     private static $_PdoDbInstance;
  16.    
  17.     /*
  18.      * Class Constructor - Create a new database connection if one doesn't exist
  19.      * Set to private so no-one can create a new instance via ' = new DB();'
  20.      */
  21.     private function __construct() {}
  22.    
  23.     /*
  24.      * Like the constructor, we make __clone private so nobody can clone the instance
  25.      */
  26.     private function __clone() {}
  27.    
  28.     /*
  29.      * Returns DB instance or create initial connection
  30.      * @param
  31.      * @return $_PdoDbInstance;
  32.      */
  33.     public static function getDB( ) {
  34.         if ( !isset(self::$_PdoDbInstance) ){
  35.             $errorString = '';
  36.             if( !defined('PDO_MYSQL_DSN') ) { $errorString .= 'PDO_MYSQL Data Source Name (DSN) hasn\'t been defined!'.PHP_EOL; }
  37.             if( !defined('MYSQL_USER')    ) { $errorString .= 'MySQL username hasn\'t been defined!'.PHP_EOL; }
  38.             if( !defined('MYSQL_PASS')    ) { $errorString .= 'MySQL password hasn\'t been defined!'.PHP_EOL; }
  39.             if( !defined('MYSQL_USER')    ) { $errorString .= 'MySQL username hasn\'t been defined!'.PHP_EOL; }
  40.             if( !empty($errorString)      ) { $errorString .= 'Class: '.__CLASS__; throw new PDOException( $errorString ); }
  41.            
  42.             // if no errors have occurred, we create an instance of the PDO database object
  43.             self::$_PdoDbInstance = new PDO( PDO_MYSQL_DSN, MYSQL_USER, MYSQL_PASS, array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' ) );
  44.             // it should throw exceptions in case of any errors
  45.             self::$_PdoDbInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  46.         }
  47.         return self::$_PdoDbInstance;      
  48.    
  49.     }
  50.    
  51.     /*
  52.      * Release database connection by setting it to NULL
  53.      * @param none
  54.      * @return none
  55.      */    
  56.     public static function setNull(){
  57.         self::$_PdoDbInstance = NULL;
  58.     }
  59. }
  60.  
  61.  
  62. /////////////////////
  63. // USING THE CLASS //
  64. /////////////////////
  65.  
  66. try {
  67.     $query = 'SELECT * FROM `test_table` WHERE test_data = :test_data_stuff ';
  68.    
  69.     $stmt = DB::getDB()->prepare($query);
  70.    
  71.     $test_data_var = 'blabla';
  72.    
  73.     $stmt->bindParam( ':test_data_stuff', $test_data_var );
  74.    
  75.     $stmt->execute();
  76.    
  77.     $assoc_array = $stmt->fetchAll(PDO::FETCH_ASSOC);
  78.    
  79.     echo '.<pre>';
  80.     print_r($assoc_array);
  81.     echo '</pre>';
  82. } catch (PDOException $e) {
  83.     DB::setNull();
  84.     echo 'Database error blah-blah...<br />';
  85.     // exception messages should rather be logged!!
  86.     echo $e->getMessage();  
  87. } catch (Exception $e) {
  88.     DB::setNull();
  89.     echo 'Any other exceptions blah-blah...<br />';
  90.     // exception messages should rather be logged!!
  91.     echo $e->getMessage();
  92. }
  93.  
  94. // THAT'S ALL!
  95.  
  96. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement