Advertisement
yesamarcos

Como desenvolver um carrinho de compra simples 3?

Mar 11th, 2016
141
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. * Mysql database class - only one connection alowed
  4. */
  5. class Conexao {
  6.    
  7.     private $_connection;
  8.     private static $_instance; //The single instance
  9.     private $_host = "localhost";
  10.     private $_username = "dev";
  11.     private $_password = "senha";
  12.     private $_database = "picasso";
  13.  
  14.     /*
  15.     Get an instance of the Database
  16.     @return Instance
  17.     */
  18.     public static function getInstance() {
  19.         if(!self::$_instance) { // If no instance then make one
  20.             self::$_instance = new self();
  21.         }
  22.         return self::$_instance;
  23.     }
  24.  
  25.     // Constructor
  26.     private function __construct() {
  27.         $this->_connection = new mysqli($this->_host, $this->_username,
  28.             $this->_password, $this->_database);
  29.    
  30.         // Error handling
  31.         if(mysqli_connect_error()) {
  32.             trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(),
  33.                  E_USER_ERROR);
  34.         }
  35.     }
  36.  
  37.     // Magic method clone is empty to prevent duplication of connection
  38.     private function __clone() { }
  39.  
  40.     // Get mysqli connection
  41.     public function getConnection() {
  42.         return $this->_connection;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement