Example #1
0
 /**
  * Connect to database
  * @return bool
  */
 public function connect($host, $user, $pass, $port = 3306, $options = array())
 {
     $client_flags = defined('MYSQL_CLIENT_FLAGS') ? MYSQL_CLIENT_FLAGS : 0;
     $this->dbh = mysqli_init();
     $socket = null;
     $port_or_socket = strstr($host, ':');
     if (!empty($port_or_socket)) {
         $host = substr($host, 0, strpos($host, ':'));
         $port_or_socket = substr($port_or_socket, 1);
         if (0 !== strpos($port_or_socket, '/')) {
             $port = intval($port_or_socket);
             $maybe_socket = strstr($port_or_socket, ':');
             if (!empty($maybe_socket)) {
                 $socket = substr($maybe_socket, 1);
             }
         } else {
             $socket = $port_or_socket;
             $port = null;
         }
     }
     if (WP_DEBUG) {
         $this->dbh->real_connect($host, $user, $pass, null, $port, $socket, $client_flags);
     } else {
         @$this->dbh->real_connect($host, $user, $pass, null, $port, $socket, $client_flags);
     }
     if (!empty($options['key']) && !empty($options['cert']) && !empty($options['ca'])) {
         $this->dbh->ssl_set($options['key'], $options['cert'], $options['ca'], $options['ca_path'], $options['cipher']);
     }
     return !mysqli_connect_error();
 }
Example #2
0
 /**
  * Establishes a connection to the defined mysql-server.
  *
  * @return void
  */
 public function connect()
 {
     if ($this->connected === TRUE) {
         return;
     }
     if ($this->configuration['db']['driver'] != 'mysql') {
         $this->logger->error('Cannot connect to a non-mysql database connection!');
         return;
     }
     $host = $this->readonly === TRUE ? $this->ro_host : $this->rw_host;
     if (isset($this->ssl_key, $this->ssl_cert, $this->ca_cert)) {
         $this->mysqli->ssl_set($this->ssl_key, $this->ssl_cert, $this->ca_cert, $this->ca_path, $this->cipher);
     }
     $this->mysqli->connect($host, $this->user, $this->pwd, $this->db, $this->port, $this->socket);
     if ($this->mysqli->errno === 0) {
         $this->mysqli->set_charset('utf8');
         $this->connected = TRUE;
     }
 }
Example #3
0
 private function _initConnection()
 {
     // return new mysqli($this->host, $this->login, $this->password, $this->database);
     $mysqli = new mysqli();
     if (isset($this->sslKeyFile) && isset($this->sslCertFile) && isset($this->sslCaFile)) {
         $mysqli->ssl_set($this->sslKeyFile, $this->sslCertFile, $this->sslCaFile, null, null);
     }
     if (!$mysqli->real_connect($this->host, $this->login, $this->password, $this->database)) {
         $this->errno = $mysqli->connect_errno;
         $this->error = $mysqli->connect_error;
     }
     return $mysqli;
 }
 public function get()
 {
     $mysql = $this->internal_mysql;
     if (!$mysql instanceof MySQLi) {
         if (!$this->port) {
             $this->port = ini_get('mysqli.default_port');
         }
         $this->current_db = $this->dbName;
         $mysql = new mysqli();
         $connect_flags = 0;
         if ($this->ssl['key']) {
             $mysql->ssl_set($this->ssl['key'], $this->ssl['cert'], $this->ssl['ca_cert'], $this->ssl['ca_path'], $this->ssl['cipher']);
             $connect_flags |= MYSQLI_CLIENT_SSL;
         }
         foreach ($this->connect_options as $key => $value) {
             $mysql->options($key, $value);
         }
         // suppress warnings, since we will check connect_error anyway
         @$mysql->real_connect($this->host, $this->user, $this->password, $this->dbName, $this->port, null, $connect_flags);
         if ($mysql->connect_error) {
             $this->nonSQLError('Unable to connect to MySQL server! Error: ' . $mysql->connect_error);
         }
         $mysql->set_charset($this->encoding);
         $this->internal_mysql = $mysql;
         $this->server_info = $mysql->server_info;
     }
     return $mysql;
 }