Advertisement
Guest User

Untitled

a guest
May 13th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @author Jon Logan
  5.  *
  6.  *
  7.  */
  8. class mySQL {
  9.     /**
  10.      * @var PDO is the only reference to the PDO Object.
  11.      */
  12.     private static $objInstance;
  13.    
  14.     /**
  15.      * @var string is the database server location.
  16.      */
  17.     private static $_host = 'localhost';
  18.    
  19.     /**
  20.      * @var string is the mySQL user.
  21.      */
  22.     private static $_username = 'thefamous';
  23.    
  24.     /**
  25.      * @var string is the mySQL password.
  26.      */
  27.     private static $_password = 'Gre9b6WfqYnuae8J';
  28.    
  29.     /**
  30.      * @var string is the database name.
  31.      */
  32.     private static $_db = 'thefamous';
  33.    
  34.     private function __construct() { }
  35.     private function __clone() {}
  36.    
  37.     /**
  38.      * Returns DB instance or create initial connection
  39.      * @param
  40.      * @return $objInstance;
  41.      * @throws MySQLConnectionException
  42.      */
  43.     public static function getInstance () {
  44.         if(!self::$objInstance){
  45.             try {
  46.             self::$objInstance = new PDO('mysql:dbname='.self::$_db.';host='.self::$_host,
  47.             self::$_username, self::$_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
  48.             self::$objInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  49.             }
  50.             catch(PDOException $e) {
  51.                 throw new MySQLConnectionException($e);
  52.             }
  53.         }
  54.         return self::$objInstance;
  55.     }
  56.    
  57.     /**
  58.      * Passes on any static calls to this class onto the singleton PDO instance
  59.      * @param $chrMethod, $arrArguments
  60.      * @return $mix
  61.      */
  62.     final public static function __callStatic($chrMethod,$arrArguments) {
  63.         $objInstance = self::getInstance();
  64.         return call_user_func_array(array($objInstance,$chrMethod),$arrArguments);
  65.     }
  66.    
  67.     /**
  68.      * Returns a PDOStatement object
  69.      * @param query
  70.      * @return PDOStatement
  71.      */
  72.     public static function prepare($a) {
  73.         return self::getInstance()->prepare($a);
  74.     }
  75.    
  76.     /**
  77.      * Returns a PDOStatement object
  78.      * @param query
  79.      * @return PDOStatement
  80.      */
  81.     public static function exec($a) {
  82.         return self::getInstance()->exec($a);
  83.     }
  84.    
  85.     /**
  86.      * Returns a PDOStatement object
  87.      * @param query
  88.      * @return PDOStatement
  89.      */
  90.     public static function execute($a) {
  91.         return self::getInstance()->exec($a);
  92.     }
  93.    
  94.    
  95.     /**
  96.      * Returns a PDOStatement object
  97.      * @param query
  98.      * @return PDOStatement
  99.      */
  100.     public static function query($a) {
  101.         $b = self::getInstance()->query($a);
  102.         return $b;
  103.     }
  104. }
  105.  
  106. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement