Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.18 KB | None | 0 0
  1. <?php
  2.  
  3. class DBInit {
  4.  
  5.     private static $host = "localhost";
  6.     private static $user = "root";
  7.     private static $password = "ep";
  8.     private static $schema = "trgovina";
  9.     private static $instance = null;
  10.  
  11.     private function __construct() {
  12.        
  13.     }
  14.  
  15.     private function __clone() {
  16.        
  17.     }
  18.  
  19.     /**
  20.      * Returns a PDO instance -- a connection to the database.
  21.      * The singleton instance assures that there is only one connection active
  22.      * at once (within the scope of one HTTP request)
  23.      *
  24.      * @return PDO instance
  25.      */
  26.     public static function getInstance() {
  27.         if (!self::$instance) {
  28.             $config = "mysql:host=" . self::$host
  29.                     . ";dbname=" . self::$schema;
  30.             $options = array(
  31.                 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  32.                 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  33.                 PDO::ATTR_PERSISTENT => true,
  34.                 PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
  35.             );
  36.  
  37.             self::$instance = new PDO($config, self::$user, self::$password, $options);
  38.         }
  39.  
  40.         return self::$instance;
  41.     }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement