Advertisement
Guest User

Untitled

a guest
Sep 5th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.55 KB | None | 0 0
  1. <?php
  2.  
  3. class Database
  4. {
  5.  
  6.     private $connection;
  7.     private static $_instance;
  8.  
  9.     private static $dbConfig = [
  10.         'host' => 'localhost',
  11.         'port' => 5433,
  12.         'database' => 'vova',
  13.         'db_user' => 'vova',
  14.         'db_pass' => 'vova'
  15.     ];
  16.  
  17.     private function __construct() {
  18.         $this->connection = new PDO(
  19.             "pgsql:dbname=" . self::$dbConfig['database'] . ";"
  20.             . "host=" . self::$dbConfig['host'] . ";"
  21.             . "port=" . self::$dbConfig['port'] . ";"
  22.             . "user=" . self::$dbConfig['db_user'] . ";"
  23.             . "password=" . self::$dbConfig['db_pass']
  24.         );
  25.     }
  26.  
  27.     public static function getInstance()
  28.     {
  29.         if (!self::$_instance) {
  30.             self::$_instance = new self();
  31.         }
  32.  
  33.         return self::$_instance;
  34.     }
  35.  
  36.     public function getConnection() {
  37.         return $this->connection;
  38.     }
  39.  
  40.     private function __clone() {}
  41. }
  42.  
  43. //////////////// Use //////////////////////////////////////////////
  44. $db = Database::getInstance(); ////////////////////////////////////
  45. $pgsql = $db->getConnection(); ////////////////////////////////////
  46. ///////////////////////////////////////////////////////////////////
  47. $query = "SELECT * FROM MyTable ORDER BY ID DESC LIMIT 10"; ///////
  48. $res = $pgsql->query($query)->fetchAll(); /////////////////////////
  49. ///////////////////////////////////////////////////////////////////
  50. print_r($res); ////////////////////////////////////////////////////
  51. ///////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement