Example #1
1
 function connect()
 {
     if ($this->connected) {
         return;
     }
     parent::init();
     parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, $this->connect_timeout);
     if ($this->persistent && version_compare(PHP_VERSION, '5.3.0') > 0) {
         $this->connected = parent::real_connect('p:' . $this->dbhost, $this->dbuser, $this->dbpassword, $this->dbname, $this->port);
     } else {
         $this->connected = parent::real_connect($this->dbhost, $this->dbuser, $this->dbpassword, $this->dbname, $this->port);
     }
     if (!$this->connected) {
         header('HTTP/1.1 503 Service Unavailable');
         die;
     }
     $this->set_charset('utf8');
     if (!$this->ban_checked) {
         // Check the IP is not banned before doing anything more
         check_ip_noaccess(2);
         // 2 == don't check in cache
         $this->ban_checked = true;
     }
     if ($this->initial_query) {
         $this->query($this->initial_query);
     }
 }
Example #2
0
 /**
  * Creates a real connection to the database with multi-query capability.
  *
  * @return void
  * @throws Zend_Db_Adapter_Mysqli_Exception
  */
 protected function _connect()
 {
     if ($this->_connection) {
         return;
     }
     if (!extension_loaded('mysqli')) {
         throw new Zend_Db_Adapter_Exception('mysqli extension is not installed');
     }
     // Suppress connection warnings here.
     // Throw an exception instead.
     @($conn = new mysqli());
     if (false === $conn || mysqli_connect_errno()) {
         throw new Zend_Db_Adapter_Mysqli_Exception(mysqli_connect_errno());
     }
     $conn->init();
     $conn->options(MYSQLI_OPT_LOCAL_INFILE, true);
     #$conn->options(MYSQLI_CLIENT_MULTI_QUERIES, true);
     $port = !empty($this->_config['port']) ? $this->_config['port'] : null;
     $socket = !empty($this->_config['unix_socket']) ? $this->_config['unix_socket'] : null;
     // socket specified in host config
     if (strpos($this->_config['host'], '/') !== false) {
         $socket = $this->_config['host'];
         $this->_config['host'] = null;
     } elseif (strpos($this->_config['host'], ':') !== false) {
         list($this->_config['host'], $port) = explode(':', $this->_config['host']);
     }
     #echo "<pre>".print_r($this->_config,1)."</pre>"; die;
     @$conn->real_connect($this->_config['host'], $this->_config['username'], $this->_config['password'], $this->_config['dbname'], $port, $socket);
     if (mysqli_connect_errno()) {
         throw new Zend_Db_Adapter_Mysqli_Exception(mysqli_connect_error());
     }
     $this->_connection = $conn;
     /** @link http://bugs.mysql.com/bug.php?id=18551 */
     $this->_connection->query("SET SQL_MODE=''");
 }
Example #3
0
 /**
  * Create a new Mysqli object.
  *
  * @param array $params
  * @return object
  */
 public function __construct($key)
 {
     mysqli_report(MYSQLI_REPORT_STRICT);
     $params = Config::get('mysql.' . $key);
     if ($params === null && IS_SUBSITE) {
         $params = MainConfig::get('mysql.' . $key);
     }
     if ($params === null) {
         $params = [];
     }
     parent::init();
     $params['pass'] = isset($params['pass']) ? $params['pass'] : '';
     $params['user'] = isset($params['user']) ? $params['user'] : 'root';
     $params['host'] = isset($params['host']) ? $params['host'] : '127.0.0.1';
     $params['port'] = isset($params['port']) ? $params['port'] : 3306;
     $params['timeout'] = isset($params['timeout']) ? $params['timeout'] : 30;
     $params['charset'] = isset($params['charset']) ? $params['charset'] : 'utf8';
     $params['database'] = isset($params['database']) ? $params['database'] : false;
     parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, $params['timeout']);
     parent::real_connect($params['host'], $params['user'], $params['pass'], $params['database'], $params['port']);
     if ($this->errno === 0) {
         $this->set_charset($params['charset']);
         if (isset($params['cache']) && $params['cache'] === false) {
             $this->cache(false);
         }
     }
 }
Example #4
0
 /**
  * {@inheritDoc}
  */
 protected function realConnect(array $parameters)
 {
     // init
     $this->link = new \mysqli();
     $this->link->init();
     // set driver specific options
     if (!empty($parameters['options'])) {
         foreach ($parameters['options'] as $option => $value) {
             $option = strtoupper($option);
             $this->attributes[$option] = $value;
         }
     }
     foreach ($this->attributes as $attr => $value) {
         if (is_string($attr)) {
             $option = constant($attr);
             if (defined($option)) {
                 $this->link->options($option, $value);
             }
         } else {
             $this->link->options($attr, $value);
         }
     }
     // real connect
     $this->link->real_connect(isset($parameters['host']) ? $parameters['host'] : 'localhost', isset($parameters['username']) ? $parameters['username'] : 'root', isset($parameters['password']) ? $parameters['password'] : null, isset($parameters['db']) ? $parameters['db'] : null, isset($parameters['port']) ? (int) $parameters['port'] : null, isset($parameters['socket']) ? $parameters['socket'] : null);
     if ($this->link->connect_error) {
         throw new LogicException(Message::get(Message::DB_CONNECT_FAIL, $this->link->connect_errno, $this->link->connect_error), Message::DB_CONNECT_FAIL);
     }
     // set charset
     if (!empty($parameters['charset'])) {
         $this->link->set_charset($parameters['charset']);
     }
     return $this;
 }
Example #5
0
 public function __construct($host, $user, $pass, $db)
 {
     parent::init();
     if (!parent::real_connect($host, $user, $pass, $db)) {
         throw new Exception('Ошибка подключения (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
     }
     if (!$this->set_charset('utf8')) {
         throw new Exception('Ошибка при загрузке набора символов utf8: ' . $this->error);
     }
 }
 /**
  * Constructor for the MySQLi Extension
  *
  * Throws an exception when connection fails
  *
  * @param $Host
  *   The hostname/ip address of the database server
  *
  * @param $Port
  *   The port on which the database server is listening
  *
  * @param $UserName
  *   A UserName which has access to the database we'll be using.
  *
  * @param $Password
  *   The Password for the UserName provided
  *
  * @param $Database
  *   The Database which contains the Neflaria tables
  *
  * @throws Exception
  */
 public function __construct($Host, $UserName, $Password, $Database, $Port)
 {
     parent::init();
     if (!parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
         $this->LogError('?', 'Setting the connect timeout failed');
     }
     if (!parent::real_connect($Host, $UserName, $Password, $Database, $Port)) {
         $this->LogError(mysqli_connect_errno(), mysqli_connect_error());
     }
 }
Example #7
0
 public function __construct($host = '127.0.0.1', $username = 'root', $password = 'root', $dbname = 'web_train')
 {
     parent::init();
     if (!parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
         die('Setting options failed');
     }
     if (!parent::real_connect($host, $username, $password, $dbname)) {
         die('Connect to db failed');
     }
     parent::query('set names utf8');
 }
Example #8
0
 /**
  * connect - Connects to mysql server
  *
  * @throws exception
  */
 public function connect()
 {
     if (!$this->connected) {
         parent::init();
         if (!parent::real_connect($this->host, $this->user, $this->pass, $this->db, $this->port)) {
             throw new Exception($this->connect_error);
         } else {
             $this->connected = true;
         }
     }
     return $this;
 }
Example #9
0
 private final function __construct()
 {
     parent::init();
     if (!parent::options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
         throw new Exception('Setting MYSQLI_INIT_COMMAND failed');
     }
     if (!parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
         throw new Exception('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
     }
     if (!parent::real_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME)) {
         throw new Exception('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
     }
 }
Example #10
0
 public function rcon($host, $user, $pass, $db)
 {
     parent::init();
     if (!parent::options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
         die('Setting MYSQLI_INIT_COMMAND failed');
     }
     if (!parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
         die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
     }
     #$db_connection = new mysqli('localhost', 'root', '', 'brechbuhler');
     if (!parent::real_connect($host, $user, $pass, $db)) {
         die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
     }
 }
Example #11
0
 public function __construct($server = 'localhost', $database = '', $username = '', $password = '', $port = '')
 {
     parent::init();
     $this->connect['server'] = $server;
     $this->connect['database'] = $database;
     $this->connect['schema'] = $database;
     // MySQL doesn't support schema's
     $this->connect['username'] = $username;
     $this->connect['password'] = $password;
     $this->connect['port'] = empty($port) ? ini_get('mysqli.default_port') : $port;
     $this->connect['persistent'] = true;
     if (!isset(self::$stats['count'])) {
         self::$stats['count'] = 0;
     }
     if (!isset(self::$stats['duration'])) {
         self::$stats['duration'] = (double) 0;
     }
 }
Example #12
0
	/**
	 * Constructor: Conecta a MySQL y establece el charset a utf8
	 * @param string $host host de MySQL
	 * @param string $user usuario de acceso a MySQL
	 * @param string $pass contraseña del usuario de acceso a MySQL
	 * @param string $db nombbre de esquema de MySQL
	 * @throws DBException si no puede conectar o establecer el charset a utf8
	 */
	public function __construct($host, $user, $pass, $db) {
		parent::init();
		/*
		if (!parent::options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
			throw new exception('Setting MYSQLI_INIT_COMMAND failed');
		}

		if (!parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
			throw new exception('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
		}
		*/
		if (!parent::real_connect($host, $user, $pass, $db)) {
			//throw new exception($this->connect_error, $this->connect_errno);
			//$connect_error falla hasta PHP 5.3.0, asi que usamos la siguiente
			throw new DBException(mysqli_connect_error(), mysqli_connect_errno());
		}
		/* change character set to utf8 */
		if (!parent::set_charset("utf8")) {
			throw new DBException(mysqli_connect_error(), mysqli_connect_errno());
		}
	}
Example #13
0
 public function __construct($host, $user, $passwd, $db, $port, $socket)
 {
     parent::init();
     parent::real_connect($host, $user, $passwd, $db, $port, $socket);
 }
Example #14
0
 /**
  * Constructor.
  *
  * $config is an array of key/value pairs
  * containing configuration options.  These options are common to most adapters:
  *
  * dbname		 => (string) The name of the database to user
  * username	   => (string) Connect to the database as this username.
  * password	   => (string) Password associated with the username.
  * host		   => (string) What host to connect to, defaults to localhost
  *
  * Some options are used on a case-by-case basis by adapters:
  *
  * port		   => (string) The port of the database
  * persistent	 => (boolean) Whether to use a persistent connection or not, defaults to false
  * protocol	   => (string) The network protocol, defaults to TCPIP
  * caseFolding	=> (int) style of case-alteration used for identifiers
  *
  * @param  array $config An array having configuration data
  * @throws Zend_Db_Adapter_Exception
  */
 public function __construct($config)
 {
     parent::init();
     $this->_config = array_merge($this->_config, $config);
     $this->_config['driver_options'] = isset($config['driver_options']) ? $config['driver_options'] : array();
     if (!empty($this->_config['options'])) {
         // obtain quoting property if there is one
         if (array_key_exists(self::AUTO_QUOTE_IDENTIFIERS, $this->_config['options'])) {
             $this->_autoQuoteIdentifiers = (bool) $this->_config['options'][self::AUTO_QUOTE_IDENTIFIERS];
         }
         // obtain auto reconnect on unserialize property if there is one
         if (array_key_exists(self::AUTO_RECONNECT_ON_UNSERIALIZE, $this->_config['options'])) {
             $this->_autoReconnectOnUnserialize = (bool) $this->_config['options'][self::AUTO_RECONNECT_ON_UNSERIALIZE];
         }
     }
     // 修改了原来的Zend_Db代码,在不开启profiler的情况下,不再生成profiler实例
     if (array_key_exists(self::PROFILER, $this->_config)) {
         if ($this->_config[self::PROFILER]) {
             $this->setProfiler($this->_config[self::PROFILER]);
         }
         unset($this->_config[self::PROFILER]);
     }
 }
Example #15
0
<?php

require_once "connect.inc";
$mysqli = new mysqli();
$mysqli->init();
$mysqli->init();
echo "done";
Example #16
-1
 /**
  * Connect
  *
  * @throws Exception\RuntimeException
  * @return $this
  */
 public function connect()
 {
     if ($this->resource instanceof \mysqli) {
         return $this;
     }
     // localize
     $p = $this->connectionParameters;
     // given a list of key names, test for existence in $p
     $findParameterValue = function (array $names) use($p) {
         foreach ($names as $name) {
             if (isset($p[$name])) {
                 return $p[$name];
             }
         }
         return;
     };
     $hostname = $findParameterValue(array('hostname', 'host'));
     $username = $findParameterValue(array('username', 'user'));
     $password = $findParameterValue(array('password', 'passwd', 'pw'));
     $database = $findParameterValue(array('database', 'dbname', 'db', 'schema'));
     $port = isset($p['port']) ? (int) $p['port'] : null;
     $socket = isset($p['socket']) ? $p['socket'] : null;
     $this->resource = new \mysqli();
     $this->resource->init();
     if (!empty($p['driver_options'])) {
         foreach ($p['driver_options'] as $option => $value) {
             if (is_string($option)) {
                 $option = strtoupper($option);
                 if (!defined($option)) {
                     continue;
                 }
                 $option = constant($option);
             }
             $this->resource->options($option, $value);
         }
     }
     $this->resource->real_connect($hostname, $username, $password, $database, $port, $socket);
     if ($this->resource->connect_error) {
         throw new Exception\RuntimeException('Connection error', null, new Exception\ErrorException($this->resource->connect_error, $this->resource->connect_errno));
     }
     if (!empty($p['charset'])) {
         $this->resource->set_charset($p['charset']);
     }
     return $this;
 }
Example #17
-1
 public function __construct($host, $user, $pass, $db)
 {
     parent::init();
     if (!parent::options(MYSQLI_INIT_COMMAND, "SET AUTOCOMMIT = 1")) {
         throw new Error("MYSQLI_INIT_COMMAND Fail");
     }
     if (!parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
         throw new Error("MYSQLI_OPT_CONNECT_TIMEOUT Fail");
     }
     if (!parent::real_connect($host, $user, $pass, $db)) {
         throw new Error("Connection ERROR. " . mysqli_connect_errno() . ": " . mysqli_connect_error());
     }
     Debug("MySQL connection established");
 }