Advertisement
miraage

Untitled

Dec 4th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.93 KB | None | 0 0
  1. <?php
  2.  
  3. class DB
  4. {
  5.     static private $_instance;
  6.  
  7.     static private $_opts = array(
  8.         PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION,
  9.         PDO::ATTR_EMULATE_PREPARES  => true,
  10.         PDO::MYSQL_ATTR_INIT_COMMAND    => 'set names utf8',
  11.     );
  12.    
  13.     static private $_conn = array(
  14.         'dsn'       => 'mysql:host=127.0.0.1;dbname=auto',
  15.         'user'      => 'root',
  16.         'passwd'    => 'root',
  17.     );
  18.    
  19.     static public function getInstance()
  20.     {
  21.         self::$_instance OR self::$_instance = new self;
  22.         return self::$_instance;
  23.     }
  24.    
  25.     public function test()
  26.     {
  27.         foreach (self::$_instance->query('SELECT id FROM blog') as $post)
  28.         {
  29.             echo $post['id'] . "\t";
  30.         }
  31.     }
  32.    
  33.     private function __construct()
  34.     {
  35.         try
  36.         {
  37.             self::$_instance = new PDO(self::$_conn['dsn'],
  38.                            self::$_conn['user'],
  39.                            self::$_conn['passwd'],
  40.                            self::$_opts);
  41.         }
  42.         catch (PDOException $e)
  43.         {
  44.             echo 'DB connection error: ' . $e->getMessage();
  45.             exit;
  46.         }
  47.     }
  48. }
  49.  
  50. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement