public function connect()
 {
     if ($this->_connection) {
         return;
     }
     if (Database_MySQLi::$_set_names === NULL) {
         // Determine if we can use mysqli_set_charset(), which is only
         // available on PHP 5.2.3+ when compiled against MySQL 5.0+
         Database_MySQLi::$_set_names = !function_exists('mysqli_set_charset');
     }
     // Extract the connection parameters, adding required variabels
     extract($this->_config['connection'] + array('database' => '', 'hostname' => '', 'port' => ini_get("mysqli.default_port"), 'socket' => ini_get("mysqli.default_socket"), 'username' => '', 'password' => '', 'persistent' => FALSE));
     // Prevent this information from showing up in traces
     //unset($this->_config['connection']['username'], $this->_config['connection']['password']);
     try {
         if ($persistent) {
             // Create a persistent connection
             $this->_connection = new mysqli('p:' . $hostname, $username, $password, $database, $port, $socket);
         } else {
             // Create a connection and force it to be a new link
             $this->_connection = new mysqli($hostname, $username, $password, $database, $port, $socket);
         }
     } catch (ErrorException $e) {
         throw new Database_Exception('[:code] :error', array(':code' => $this->_connection->connect_errno, ':error' => $this->_connection->connect_error), $this->_connection->connect_errno);
         // No connection exists
         $this->_connection = NULL;
     }
     // \xFF is a better delimiter, but the PHP driver uses underscore
     $this->_connection_id = sha1($hostname . '_' . $username . '_' . $password);
     $this->_select_db($database);
     if (!empty($this->_config['charset'])) {
         // Set the character set
         $this->set_charset($this->_config['charset']);
     }
 }
Example #2
0
 public function connect()
 {
     if ($this->_connection) {
         return;
     }
     if (Database_MySQLi::$_set_names === NULL) {
         // Determine if we can use mysqli_set_charset(), which is only
         // available on PHP 5.2.3+ when compiled against MySQL 5.0+
         Database_MySQLi::$_set_names = !function_exists('mysqli_set_charset');
     }
     // Extract the connection parameters, adding required variabels
     extract($this->_config['connection'] + array('database' => '', 'hostname' => '', 'username' => '', 'password' => '', 'persistent' => FALSE));
     // Prevent this information from showing up in traces
     unset($this->_config['connection']['username'], $this->_config['connection']['password']);
     try {
         if ($persistent) {
             // Create a persistent connection
             $this->_connection = mysqli_connect('p:' . $hostname, $username, $password);
         } else {
             // Create a connection and force it to be a new link
             $this->_connection = mysqli_connect($hostname, $username, $password);
         }
     } catch (Exception $e) {
         // No connection exists
         $this->_connection = NULL;
         throw new Database_Exception(':error', array(':error' => $e->getMessage()), $e->getCode());
     }
     // \xFF is a better delimiter, but the PHP driver uses underscore
     $this->_connection_id = sha1($hostname . '_' . $username . '_' . $password);
     $this->_select_db($database);
     if (!empty($this->_config['charset'])) {
         // Set the character set
         $this->set_charset($this->_config['charset']);
     }
     if (!empty($this->_config['connection']['variables'])) {
         // Set session variables
         $variables = array();
         foreach ($this->_config['connection']['variables'] as $var => $val) {
             $variables[] = 'SESSION ' . $var . ' = ' . $this->quote($val);
         }
         mysqli_query($this->_connection, 'SET ' . implode(', ', $variables));
     }
 }
 public function connect()
 {
     if ($this->_connection) {
         return;
     }
     if (Database_MySQLi::$_set_names === NULL) {
         // Determine if we can use mysqli_set_charset(), which is only
         // available on PHP 5.2.3+ when compiled against MySQL 5.0+
         Database_MySQLi::$_set_names = !function_exists('mysqli_set_charset');
     }
     // Extract the connection parameters, adding required variabels
     extract($this->_config['connection'] + array('database' => '', 'hostname' => '', 'username' => '', 'password' => '', 'socket' => '', 'port' => 3306, 'ssl' => NULL));
     // Prevent this information from showing up in traces
     unset($this->_config['connection']['username'], $this->_config['connection']['password']);
     try {
         if (is_array($ssl)) {
             $this->_connection = mysqli_init();
             $this->_connection->ssl_set(Arr::get($ssl, 'client_key_path'), Arr::get($ssl, 'client_cert_path'), Arr::get($ssl, 'ca_cert_path'), Arr::get($ssl, 'ca_dir_path'), Arr::get($ssl, 'cipher'));
             $this->_connection->real_connect($hostname, $username, $password, $database, $port, $socket, MYSQLI_CLIENT_SSL);
         } else {
             $this->_connection = new mysqli($hostname, $username, $password, $database, $port, $socket);
         }
     } catch (Exception $e) {
         // No connection exists
         $this->_connection = NULL;
         throw new Database_Exception(':error', array(':error' => $e->getMessage()), $e->getCode());
     }
     // \xFF is a better delimiter, but the PHP driver uses underscore
     $this->_connection_id = sha1($hostname . '_' . $username . '_' . $password);
     if (!empty($this->_config['charset'])) {
         // Set the character set
         $this->set_charset($this->_config['charset']);
     }
     if (!empty($this->_config['connection']['variables'])) {
         // Set session variables
         $variables = array();
         foreach ($this->_config['connection']['variables'] as $var => $val) {
             $variables[] = 'SESSION ' . $var . ' = ' . $this->quote($val);
         }
         $this->_connection->query('SET ' . implode(', ', $variables));
     }
 }