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('mysql_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 = @mysql_pconnect($hostname, $username, $password); } else { // Create a connection and force it to be a new link $this->_connection = @mysql_connect($hostname, $username, $password, TRUE); } } catch (ErrorException $e) { // No connection exists $this->_connection = NULL; throw new Database_Exception(mysql_errno(), '[:code] :error', array(':code' => mysql_errno(), ':error' => mysql_error())); } // \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']); } }
public function connect() { if ($this->_connection) { return; } if (Database_MySQL::$_set_names === NULL) { Database_MySQL::$_set_names = !function_exists('mysql_set_charset'); } $cfg = $this->_config['connection'] + array('database' => '', 'hostname' => '', 'username' => '', 'password' => '', 'persistent' => FALSE); extract($cfg); unset($this->_config['connection']['username'], $this->_config['connection']['password']); try { if ($persistent) { $this->_connection = mysql_pconnect($hostname, $username, $password); } else { $this->_connection = mysql_connect($hostname, $username, $password, TRUE); } } catch (Exception $e) { $this->_connection = NULL; throw new Database_Exception(':error', array(':error' => $e->getMessage()), $e->getCode()); } $this->_connection_id = sha1($hostname . '_' . $username . '_' . $password); $this->_select_db($database); if (!empty($this->_config['charset'])) { $this->set_charset($this->_config['charset']); } if (!empty($this->_config['connection']['variables'])) { $variables = array(); foreach ($this->_config['connection']['variables'] as $var => $val) { $variables[] = 'SESSION ' . $var . ' = ' . $this->quote($val); } mysql_query('SET ' . implode(', ', $variables), $this->_connection); } }
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('mysql_set_charset'); } // Extract the connection parameters, adding required variabels /** * @var string $database * @var string $hostname * @var string $username * @var string $password * @var bool $persistent */ extract($this->_config['connection'] + ['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 = mysql_pconnect($hostname, $username, $password); } else { // Create a connection and force it to be a new link $this->_connection = mysql_connect($hostname, $username, $password, true); } } catch (Exception $e) { // No connection exists $this->_connection = null; throw new Database_Exception(':error', [':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 = []; foreach ($this->_config['connection']['variables'] as $var => $val) { $variables[] = 'SESSION ' . $var . ' = ' . $this->quote($val); } mysql_query('SET ' . implode(', ', $variables), $this->_connection); } }
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('mysql_set_charset'); } // Extract the connection parameters, adding required variabels extract($this->_config['connection'] + array('database' => '', 'hostname' => '', 'port' => NULL, 'socket' => NULL, 'username' => '', 'password' => '', 'persistent' => FALSE)); // Clear the connection parameters for security unset($this->_config['connection']); try { if (empty($persistent)) { // Create a connection and force it to be a new link $this->_connection = mysql_connect($hostname, $username, $password, true); } else { // Create a persistent connection $this->_connection = mysql_pconnect($hostname, $username, $password); } } catch (ErrorException $e) { // No connection exists $this->_connection = NULL; // Unable to connect to the database throw new Database_Exception(':error', array(':error' => mysql_error()), mysql_errno()); } if (!mysql_select_db($database, $this->_connection)) { // Unable to select database throw new Database_Exception(':error', array(':error' => mysql_error($this->_connection)), mysql_errno($this->_connection)); } if (!empty($this->_config['charset'])) { // Set the character set $this->set_charset($this->_config['charset']); } }
public function connect($master = FALSE) { // Check if a connection to a master or slave connection // has already been made and return it. if ($master === TRUE and is_resource($this->_db_master)) { $this->_connection = $this->_db_master; $this->_connection_id = $this->_db_master_connection_id; return; } elseif ($master === FALSE and is_resource($this->_db_slave)) { $this->_connection = $this->_db_slave; $this->_connection_id = $this->_db_slave_connection_id; 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('mysql_set_charset'); } // Extract the connection parameters, adding required variabels extract($this->_config['connection'] + array('database' => '', 'hostname' => '', 'port' => NULL, 'socket' => NULL, 'username' => '', 'password' => '', 'persistent' => FALSE)); // Pick a database from the master pool if ($master === TRUE) { $index = array_rand($hostname['master']); $hostname = $hostname['master'][$index]; } else { $index = array_rand($hostname['slave']); $hostname = $hostname['slave'][$index]; } // Prevent this information from showing up in traces // unset($this->_config['connection']['username'], $this->_config['connection']['password']); try { if (empty($persistent)) { // Create a connection and force it to be a new link $this->_connection = mysql_connect($hostname, $username, $password, TRUE); } else { // Create a persistent connection $this->_connection = mysql_pconnect($hostname, $username, $password); } // Set the master connection if ($master === TRUE) { $this->_db_master = $this->_connection; } else { $this->_db_slave = $this->_connection; } } catch (ErrorException $e) { // No connection exists $this->_connection = $this->_db_master = $this->_db_slave = NULL; throw $e; } // \xFF is a better delimiter, but the PHP driver uses underscore $this->_connection_id = sha1($hostname . '_' . $username . '_' . $password); if ($master === TRUE) { $this->_db_master_connection_id = $this->_connection_id; } else { $this->_db_slave_connection_id = $this->_connection_id; } $this->_select_db($database); if (!empty($this->_config['charset'])) { // Set the character set $this->set_charset($this->_config['charset']); } }