Example #1
1
 public function connect()
 {
     if ($this->conn_id && $this->state == self::OPEN) {
         mysqli_select_db($this->conn_id, $this->getDatabase());
         return true;
     }
     //TODO preConnect actions should be called from here
     $hostString = $this->getHost();
     if ($this->getPort() != '') {
         $hostString .= ':' . $this->getPort();
     }
     if (isset($this->options['socket']) && $this->options['socket'] != '') {
         $hostString .= ':' . $this->options['socket'];
     }
     $flags = isset($this->options['flags']) ? $this->options['flags'] : null;
     if (isset($this->options['persistent']) && $this->options['persistent'] == true) {
         $this->conn_id = @mysqli_pconnect($hostString, $this->getUser(), $this->getPassword(), $flags);
     } else {
         $this->conn_id = @mysqli_connect($hostString, $this->getUser(), $this->getPassword(), $flags);
     }
     if (!$this->conn_id) {
         $this->state = self::CLOSED;
         $msg = '[!Database connection error!]: ' . $this->getDatabase() . ' - ' . $this->getErrorMsg();
         PhpBURN_Message::output($msg, PhpBURN_Message::ERROR);
         return false;
     }
     //Selecting database
     mysqli_select_db($this->conn_id, $this->getDatabase());
     $this->state = self::OPEN;
     //TODO onConnectSucess actions should be called from here
     return true;
 }
Example #2
0
 function connect($db)
 {
     if ($db['port']) {
         $db['server'] .= ':' . $port;
     }
     if ($this->link_identifier) {
         $this->disconnect();
     }
     $this->link_identifier = $db['persistent'] ? @mysqli_pconnect($db['server'], $db['username'], $db['password']) : @mysqli_connect($db['server'], $db['username'], $db['password']);
     if ($this->link_identifier) {
         if (@mysqli_select_db($this->link_identifier, $db['database'])) {
             mysqli_query($this->link_identifier, 'SET NAMES utf8');
             //mysqli_query($this->link_identifier, 'SET character_set_results = NULL');
             return $this->link_identifier;
         }
         $error = '<center>There is currently a problem with the site<br/>Please try again later<br /><br />Error Code: DB2</center>';
     }
     if (!$this->report_error) {
         return false;
     }
     if (!isset($error)) {
         $error = '<center>There is currently a problem with the site<br/>Please try again later<br /><br />Error Code: DB1</center>';
     }
     $this->disconnect();
     trigger_error($error, E_USER_ERROR);
     die;
 }
Example #3
0
 function conectar($server = IP_SERVER, $user = USER_DB, $pass = PASSWORD_DB, $based = DB, $puerto = PORT, $tipo_conexion = "N")
 {
     try {
         switch ($tipo_conexion) {
             case "N":
                 // Conexion a B.D no persistente
                 $this->id_conexion = @mysqli_connect($server, $user, $pass, $based, $puerto);
                 break;
             case "P":
                 // Conexion a B.D persistente
                 $this->id_conexion = @mysqli_pconnect($server, $user, $pass, $based, $puerto);
                 break;
             default:
                 // Otros casos
                 $this->id_conexion = @mysqli_connect($server, $user, $pass, $based, $puerto);
                 break;
         }
         if ($this->id_conexion) {
             $this->id_conexion->set_charset(CODEC);
             return $this->id_conexion;
         } else {
             throw new Exception("Error 01: " . ($this->id_conexion ? mysqli_error($this->id_conexion) : 'Servidor o B.D. no disponible'));
         }
     } catch (Exception $e) {
         $this->error1 = $e->getMessage();
         return null;
     }
 }
Example #4
0
 public function connect()
 {
     if ($this->conn == 'pconn') {
         $this->conn = mysqli_pconnect($this->host, $this->user, $this->password);
         $this->db = mysql_select_db($this->db);
         mysql_query("set names {$this->coding}");
     } else {
         $this->conn = mysql_connect($this->host, $this->user, $this->password);
         $this->db = mysql_select_db($this->db);
         mysql_query("SET NAMES {$this->coding}");
     }
 }
Example #5
0
 /**
  * MYSQL连接器
  * @param  string $host sql服务器
  * @param  string $user 数据库用户名
  * @param  string $password 数据库登录密码
  * @param  string $database 数据库
  * @param  string $charset 编码
  * @param  string $pconnect 是否持久链接
  * @return obj
  */
 public function connect($host, $user, $password, $database, $charset = 'utf8', $pconnect = 0)
 {
     $link_id = $pconnect == 0 ? mysqli_connect($host, $user, $password) : mysqli_pconnect($host, $user, $password);
     if (!$link_id) {
         InitPHP::initError('mysql connect error!');
     }
     mysqli_query($link_id, 'SET NAMES ' . $charset);
     if (!mysqli_select_db($link_id, $database)) {
         InitPHP::initError('database is not exist!');
     }
     return $link_id;
 }
Example #6
0
 function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false)
 {
     $this->persistency = $persistency;
     $this->user = $sqluser;
     $this->server = $sqlserver . ($port ? ':' . $port : '');
     $this->dbname = $database;
     $this->db_connect_id = $this->persistency ? @mysqli_pconnect($this->server, $this->user, $sqlpassword) : @mysqli_connect($this->server, $this->user, $sqlpassword);
     if ($this->db_connect_id && $this->dbname != '') {
         if (@mysqli_select_db($this->db_connect_id, $this->dbname)) {
             return $this->db_connect_id;
         }
     }
     return $this->sql_error('');
 }
Example #7
0
 /** Connects to a database.
  * @return void
  * @throws DatabaseException
  */
 public function connect(array $config)
 {
     $this->config = $config;
     if (isset($config['resource'])) {
         $this->connection = $config['resource'];
     } else {
         if (!isset($config['charset'])) {
             $config['charset'] = 'utf8';
         }
         if (!isset($config['username'])) {
             $config['username'] = ini_get('mysql.default_user');
         }
         if (!isset($config['password'])) {
             $config['password'] = ini_get('mysql.default_password');
         }
         if (!isset($config['host'])) {
             $host = ini_get('mysql.default_host');
             if ($host) {
                 $config['host'] = $host;
                 $config['port'] = ini_get('mysql.default_port');
             } else {
                 if (!isset($config['socket'])) {
                     $config['socket'] = ini_get('mysql.default_socket');
                 }
                 $config['host'] = NULL;
             }
         }
         $host = empty($config['socket']) ? $config['host'] . (empty($config['port']) ? '' : ':' . $config['port']) : ($host = ':' . $config['socket']);
         $this->connection = empty($config['persistent']) ? @mysqli_connect($host, $config['username'], $config['password']) : @mysqli_pconnect($host, $config['username'], $config['password']);
     }
     if (!$this->is_connected()) {
         throw new \System\Error\Database('Could not connect to database "' . $config['database'] . '" for following reasons.');
     }
     $this->select_db($config['database']);
     if (isset($config['charset'])) {
         $ok = FALSE;
         if (function_exists('mysql_set_charset')) {
             // affects the character set used by mysql_real_escape_string() (was added in MySQL 5.0.7 and PHP 5.2.3)
             $ok = @mysqli_set_charset($this->connection, $config['charset']);
             // intentionally @
         }
         !$ok && $this->query("SET NAMES '{$config['charset']}'");
     }
     if (isset($config['sqlmode'])) {
         $this->query("SET sql_mode='{$config['sqlmode']}'");
     }
     $this->query("SET time_zone='" . date('P') . "'");
     $this->buffered = empty($config['unbuffered']);
 }
Example #8
0
 /**
  * MYSQL连接器
  * @param  string $host sql服务器
  * @param  string $user 数据库用户名
  * @param  string $password 数据库登录密码
  * @param  string $database 数据库
  * @param  string $charset 编码
  * @param  string $pconnect 是否持久链接
  * @return obj
  */
 public function connect($host, $user, $password, $database, $charset = 'utf8', $pconnect = 0)
 {
     $port = 3306;
     //设置默认值
     if (strpos($host, ":")) {
         $arr = explode(":", $host);
         $host = $arr[0];
         $port = $arr[1];
     }
     $link_id = $pconnect == 0 ? mysqli_connect($host, $user, $password, $database, $port) : mysqli_pconnect($host, $user, $password, $database, $port);
     if (!$link_id) {
         InitPHP::initError("mysqli connect error");
     }
     mysqli_query($link_id, 'SET NAMES ' . $charset);
     return $link_id;
 }
Example #9
0
 /**
  * Make a Connect to the mysql Server
  *
  * @param string $host
  * @param string $user
  * @param string $pass
  * @param string $db
  * @param string $schema not required
  * @param string $enconding not required
  * @return mixed return connection resource
  */
 function connect($host, $user, $pass, $db, $schema = "", $encoding = "")
 {
     if ($this->CLASS['config']->db->pcconect) {
         $this->connection = mysqli_pconnect($host, $user, $pass);
     } else {
         $this->connection = mysqli_connect($host, $user, $pass);
     }
     if (!$this->connection) {
         $this->CLASS['error']->log("Cannnot connect to host!", 1, "class-mysqli.php::connect");
         exit;
     }
     $conndb = mysqli_select_db($this->connection, $db);
     if (!$conndb) {
         $this->CLASS['error']->log("Wrong Database!", 1, "class-mysqli.php::connect");
         exit;
     }
     return $this->connection;
 }
Example #10
0
 public function connect()
 {
     if ($this->_connection) {
         return;
     }
     if (Database_MySQL::$_set_names === NULL) {
         // Determine if we can use mysql_set_charset(), which is only
         // available on PHP 5.2.3+ when compiled against MySQL 5.0+
         Database_MySQL::$_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_pconnect($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('SET ' . implode(', ', $variables), $this->_connection);
     }
 }
Example #11
0
 /**
  * Connect to specified MySQL server
  *
  * @param string $database (Optional) Database name
  * @param string $server   (Optional) Host address
  * @param string $username (Optional) User name
  * @param string $password (Optional) Password
  * @param string $charset  (Optional) Character set
  * @param boolean $pcon    (Optional) Persistant connection
  * @return boolean Returns TRUE on success or FALSE on error
  */
 public function Open($database = null, $server = null, $username = null, $password = null, $charset = null, $pcon = false)
 {
     $this->ResetError();
     // Use defaults?
     if ($database !== null) {
         $this->db_dbname = $database;
     }
     if ($server !== null) {
         $this->db_host = $server;
     }
     if ($username !== null) {
         $this->db_user = $username;
     }
     if ($password !== null) {
         $this->db_pass = $password;
     }
     if ($charset !== null) {
         $this->db_charset = $charset;
     }
     if (is_bool($pcon)) {
         $this->db_pcon = $pcon;
     }
     $this->active_row = -1;
     // Open persistent or normal connection
     if ($pcon) {
         $this->mysql_link = @mysqli_pconnect('p:' . $this->db_host, $this->db_user, $this->db_pass);
     } else {
         $this->mysql_link = @mysqli_connect($this->db_host, $this->db_user, $this->db_pass);
     }
     // Connect to mysql server failed?
     if (!$this->IsConnected()) {
         $this->SetError();
         return false;
     } else {
         // Select a database (if specified)
         if (strlen($this->db_dbname) > 0) {
             if (strlen($this->db_charset) == 0) {
                 if (!$this->SelectDatabase($this->db_dbname)) {
                     return false;
                 } else {
                     return true;
                 }
             } else {
                 if (!$this->SelectDatabase($this->db_dbname, $this->db_charset)) {
                     return false;
                 } else {
                     return true;
                 }
             }
         } else {
             return true;
         }
     }
 }
 public function open($persistent = true)
 {
     if (!isset($this->_connection) || isset($this->_connection) && !$this->isConnected()) {
         $this->_addTraceInfo(__FILE__, __METHOD__, __LINE__, 'Opening Database Connection...');
         $this->_connection = mysqli_pconnect($this->server, $this->username, $this->password, '');
         if (!$this->_connection) {
             $this->_addTraceInfo(__FILE__, __METHOD__, __LINE__, 'Unable to establish a database connection.');
         }
         mysqli_select_db($this->_connection, $this->database);
         //            $this->execute('SET SESSION wait_timeout = 60');
     }
 }
Example #13
0
 /**
  * 连接数据库
  *
  * author wj
  * param  string  $dbhost   数据库主机名
  * param  string  $dbuser   数据库用户名
  * param  string  $dbpw     数据库密码
  * param  string  $charset  数据库字符集
  * param  string  $pconnect 持久链接,1为开启,0为关闭
  * param  string  $quiet    安静模式,1为开启,0为关闭
  * return bool
  **/
 function connect($dbhost, $dbport, $dbuser, $dbpw, $dbname = '', $charset = 'utf8', $pconnect = 0, $quiet = 0)
 {
     if ($pconnect) {
         if (!($this->_link_id = @mysqli_pconnect('p:' . $dbhost, $dbuser, $dbpw, $dbname, $dbport))) {
             if (!$quiet) {
                 $this->ErrorMsg();
             }
             return false;
         }
     } else {
         $this->_link_id = mysqli_connect($dbhost, $dbuser, $dbpw, $dbname, $dbport);
         if (PHP_VERSION < '4.2') {
             mt_srand((double) microtime() * 1000000);
             // 对 PHP 4.2 以下的版本进行随机数函数的初始化工作
         }
         if (!$this->_link_id) {
             if (!$quiet) {
                 $this->ErrorMsg();
             }
             return false;
         }
     }
     $this->_dbhash = md5(WEB_PATH . $dbhost . $dbuser . $dbpw . $dbname);
     $this->version = mysqli_get_server_info($this->_link_id);
     /* 如果mysql 版本是 4.1+ 以上,需要对字符集进行初始化 */
     if ($this->version > '4.1') {
         if ($charset != 'latin1') {
             mysqli_query($this->_link_id, "SET character_set_connection={$charset}, character_set_results={$charset}, character_set_client=binary");
         }
         if ($this->version > '5.0.1') {
             mysqli_query($this->_link_id, "SET sql_mode=''");
         }
     }
     $sqlcache_config_file = $this->cache_dir . 'config_file_' . $this->_dbhash . '.php';
     if (is_file($sqlcache_config_file)) {
         include $sqlcache_config_file;
     }
     $this->starttime = time();
     if ($this->max_cache_time && $this->starttime > $this->mysql_config_cache_file_time + $this->max_cache_time) {
         if ($dbhost != '.') {
             strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? $this->_platform = 'WINDOWS' : ($this->_platform = 'OTHER');
             /*$result = mysqli_query($this->_link_id, "SHOW VARIABLES LIKE 'basedir'");
               $row    = mysqli_fetch_assoc($result);
               if (!empty($row['Value']{1}) && $row['Value']{1} == ':' && !empty($row['Value']{2}) && $row['Value']{2} == "\\")
               {
                   $this->_platform = 'WINDOWS';
               }
               else
               {
                   $this->_platform = 'OTHER';
               }*/
         } else {
             $this->_platform = 'WINDOWS';
         }
         if ($this->_platform == 'OTHER' && ($dbhost != '.' && strtolower($dbhost) != 'localhost:3306' && $dbhost != '127.0.0.1:3306') || PHP_VERSION >= '5.1' && date_default_timezone_get() == 'UTC') {
             $result = mysqli_query($this->_link_id, "SELECT UNIX_TIMESTAMP() AS timeline, UNIX_TIMESTAMP('" . date('Y-m-d H:i:s', $this->starttime) . "') AS timezone");
             $row = mysqli_fetch_assoc($result);
             if ($dbhost != '.' && strtolower($dbhost) != 'localhost:3306' && $dbhost != '127.0.0.1:3306') {
                 $this->timeline = $this->starttime - $row['timeline'];
             }
             if (PHP_VERSION >= '5.1' && date_default_timezone_get() == 'UTC') {
                 $this->timezone = $this->starttime - $row['timezone'];
             }
         }
         $content = '<' . "?php\r\n" . '$this->mysql_config_cache_file_time = ' . $this->starttime . ";\r\n" . '$this->timeline = ' . $this->timeline . ";\r\n" . '$this->timezone = ' . $this->timezone . ";\r\n" . '$this->_platform = ' . "'" . $this->_platform . "';\r\n?" . '>';
         @file_put_contents($sqlcache_config_file, $content, LOCK_EX);
     }
     /* 选择数据库 */
     if ($dbname) {
         if (mysqli_select_db($this->_link_id, $dbname) === false) {
             if (!$quiet) {
                 $this->ErrorMsg();
             }
             return false;
         } else {
             return true;
         }
     } else {
         return true;
     }
 }
 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
 {
     if (!empty($this->port)) {
         $argHostname .= ":" . $this->port;
     }
     if (ADODB_PHPVER >= 0x4300) {
         $this->_connectionID = mysqli_pconnect($argHostname, $argUsername, $argPassword, $this->clientFlags);
     } else {
         $this->_connectionID = mysqli_pconnect($argHostname, $argUsername, $argPassword);
     }
     if ($this->_connectionID === false) {
         return false;
     }
     if ($this->autoRollback) {
         $this->RollbackTrans();
     }
     if ($argDatabasename) {
         return $this->SelectDB($argDatabasename);
     }
     return true;
 }
Example #15
0
/**
 * SQL function
 */
function sql_native($sql_string)
{
    global $config, $db_native;
    if (!is_resource($db_native)) {
        $db_native = mysqli_pconnect($config['db_server'], $config['db_user'], $config['db_password'], $config['db_database']);
        if (mysqli_connect_error()) {
            errorpage('DB Connection Error', "<p>Edit the database settings in <code>" . CONFIG_FILE . "</code>.</p>\n                       <p>Alternatively, consider running the <a href='install.php'>installation script</a>.</p>");
        }
    }
    $res = mysqli_query($db_native, $sql_string);
    // mysqli_db_query returns either positive result ressource or true/false for an insert/update statement
    if ($res === false) {
        // report DB Problem
        errorpage('Database Problem', mysqli_error($db_native) . "\n<br />\n" . $sql_string);
    } elseif ($res === true) {
        // on insert, return id of created record
        $result = mysqli_insert_id($db_native);
    } else {
        // return associative result array
        $result = array();
        for ($i = 0; $i < mysqli_num_rows($res); $i++) {
            $result[] = mysqli_fetch_assoc($res);
        }
        mysqli_free_result($res);
    }
    return $result;
}