Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.08 KB | None | 0 0
  1. <?php
  2. # ----------------------------------------
  3. # Connection variables
  4. # ----------------------------------------
  5. define('HOST',     '127.0.0.1');
  6. define('DBNAME',   'database');
  7. define('CHARSET',  'utf8');
  8. define('USER',     'user');
  9. define('PASSWORD', 'password');
  10. # ----------------------------------------
  11. # Connection class
  12. # ----------------------------------------
  13. class Connection
  14. {
  15.   private static $pdo;             # PDO instance
  16.  private function __construct(){} # Hiding class constructor
  17.  
  18.   # ----------------------------------------
  19.  # Create connection
  20.  # ----------------------------------------
  21.  public static function getInstance()
  22.   {
  23.     if (!isset(self::$pdo))
  24.     {
  25.       try
  26.       {
  27.         $conn_options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8', PDO::ATTR_PERSISTENT => true);
  28.         self::$pdo    = new PDO('mysql:host='.HOST.';dbname='.DBNAME.';charset='.CHARSET, USER, PASSWORD, $conn_options);
  29.       }
  30.       catch (PDOException $e)
  31.       {
  32.         echo 'Erro: '.$e->getMessage();
  33.       }
  34.     }
  35.     return self::$pdo;
  36.   }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement