Don't like ads? PRO users don't see any ads ;-)
Guest

Connect

By: a guest on Apr 26th, 2012  |  syntax: PHP  |  size: 1.05 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. /**
  4.  * @todo abstratir para varios bancos usando PDO
  5.  * */
  6. class Connect {
  7.  
  8.   private static $_instance = NULL;
  9.   private $_connection;
  10.  
  11.   /**
  12.    * comentar
  13.    * */
  14.   prviate function __construct ($hostname, $username, $password, $database) {
  15.    
  16.     if (!($this->_connection = mysql_connect($hostname, $username, $password))){
  17.       throw new Exception('Não foi possível conectar-se ao banco de dados!');
  18.     }
  19.  
  20.     if (!(mysql_select_db($database, $this->_connection))){
  21.       throw new Exception("A base de dados <strong>{$database}</strong> não foi encontrada!");
  22.     }
  23.  
  24.   }
  25.  
  26.   public function connection(){
  27.     return $this->_connection;
  28.   }
  29.  
  30.   public static function factory ($hostname, $username, $password, $database)
  31.   {
  32.       if (NULL === self::$_instance) {
  33.           self::$_instance = new self($hostname, $username, $password, $database);
  34.       }
  35.  
  36.       return self::$_instance;
  37.   }
  38. }
  39.  
  40.  
  41. $link = Connect::factory('...param...');
  42. $sql = "SELECT * FROM pessoas";
  43. $result = mysql_query($sql, $link->connection());