/** * Connect to a MySQL database. * @param array $parameters An map of parameters, which should include: * - server: The server, eg, localhost * - username: The username to log on with * - password: The password to log on with * - database: The database to connect to * - timezone: (optional) The timezone offset. For example: +12:00, "Pacific/Auckland", or "SYSTEM" */ public function __construct($parameters) { $this->dbConn = new MySQLi($parameters['server'], $parameters['username'], $parameters['password']); if ($this->dbConn->connect_error) { $this->databaseError("Couldn't connect to MySQL database | " . $this->dbConn->connect_error); } $this->query("SET sql_mode = 'ANSI'"); if (self::$connection_charset) { $this->dbConn->set_charset(self::$connection_charset); } $this->active = $this->dbConn->select_db($parameters['database']); $this->database = $parameters['database']; if (isset($parameters['timezone'])) { $this->query(sprintf("SET SESSION time_zone = '%s'", $parameters['timezone'])); } }
/** * Connect to a MySQL database. * @param array $parameters An map of parameters, which should include: * - server: The server, eg, localhost * - username: The username to log on with * - password: The password to log on with * - database: The database to connect to * - timezone: (optional) The timezone offset. For example: +12:00, "Pacific/Auckland", or "SYSTEM" */ public function __construct($parameters) { $this->dbConn = new MySQLi($parameters['server'], $parameters['username'], $parameters['password']); if ($this->dbConn->connect_error) { $this->databaseError("Couldn't connect to MySQL database | " . $this->dbConn->connect_error); } /* $this->query("SET sql_mode = 'ANSI'"); */ /* Risoluzione errore "only_full_group_by" (http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_ansi) */ $this->query("SET sql_mode = 'REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'"); if (self::$connection_charset) { $this->dbConn->set_charset(self::$connection_charset); } $this->active = $this->dbConn->select_db($parameters['database']); $this->database = $parameters['database']; if (isset($parameters['timezone'])) { $this->query(sprintf("SET SESSION time_zone = '%s'", $parameters['timezone'])); } }
/** * @dataProvider data_strip_invalid_text * @ticket 21212 */ function test_strip_invalid_text($data, $expected, $message) { if (version_compare(PHP_VERSION, '5.3', '<') && stristr(php_uname('s'), 'win')) { $this->markTestSkipped('This test fails in PHP 5.2 on Windows. See https://core.trac.wordpress.org/ticket/31262'); } $charset = self::$_wpdb->charset; if (isset($data[0]['connection_charset'])) { $new_charset = $data[0]['connection_charset']; unset($data[0]['connection_charset']); } else { $new_charset = $data[0]['charset']; } self::$_wpdb->charset = $new_charset; self::$_wpdb->set_charset(self::$_wpdb->dbh, $new_charset); $actual = self::$_wpdb->strip_invalid_text($data); self::$_wpdb->charset = $charset; self::$_wpdb->set_charset(self::$_wpdb->dbh, $charset); $this->assertSame($expected, $actual, $message); }
/** * Connect to a MySQL database. * @param array $parameters An map of parameters, which should include: * - server: The server, eg, localhost * - username: The username to log on with * - password: The password to log on with * - database: The database to connect to * - timezone: (optional) The timezone offset. For example: +12:00, "Pacific/Auckland", or "SYSTEM" */ public function __construct($parameters) { if (!empty($parameters['port'])) { $this->dbConn = new MySQLi($parameters['server'], $parameters['username'], $parameters['password'], '', $parameters['port']); } else { $this->dbConn = new MySQLi($parameters['server'], $parameters['username'], $parameters['password']); } if ($this->dbConn->connect_error) { $this->databaseError("Couldn't connect to MySQL database | " . $this->dbConn->connect_error); } $this->query("SET sql_mode = 'ANSI'"); if (Config::inst()->get('MySQLDatabase', 'connection_charset')) { $this->dbConn->set_charset(Config::inst()->get('MySQLDatabase', 'connection_charset')); } $this->active = $this->dbConn->select_db($parameters['database']); $this->database = $parameters['database']; if (isset($parameters['timezone'])) { $this->query(sprintf("SET SESSION time_zone = '%s'", $parameters['timezone'])); } }
/** * Set character set of the MySQL-connection. * * Trys to set the connection charset and returns it. * Throw Exception on failure. * * @param string $charset * @throws Exception * * @return string */ public function setConnectionCharset($charset = 'utf8') { if (!$this->_mysqli instanceof mysqli) { $this->dbConnect(); } if (!@$this->_mysqli->set_charset($charset)) { $this->sqlError($charset . ' ' . $this->_mysqli->error, $this->_mysqli->errno); } $this->connectionCharset = $this->_mysqli->character_set_name(); return $this->connectionCharset; }
/** * Try to connect to the database */ public function connect() { $this->mysqli = new Mysqli($this->config['host'], $this->config['user'], $this->config['password'], $this->config['name']); if ($this->mysqli->connect_errno) { printf("Connect failed: %s\n", $this->mysqli->connect_error); exit; } if ($this->config['charset']) { $this->mysqli->set_charset($this->config['charset']); } return $this; }
/** * Hace una conexión a la base de datos de MySQL * * @param array $config * @return resource_connection */ public function connect($config) { if (!extension_loaded('mysqli')) { throw new KumbiaException('Debe cargar la extensión de PHP llamada php_mysqli'); } $this->id_connection = new mysqli($config['host'], $config['username'], $config['password'], $config['name'], $config['port']); //no se usa $object->error() ya que solo funciona a partir de 5.2.9 y 5.3 if (mysqli_connect_error()) { throw new KumbiaException(mysqli_connect_error()); } //Selecciona charset if (isset($config['charset'])) { $this->id_connection->set_charset($config['charset']); } return TRUE; }
/** * @dataProvider data_strip_invalid_text * @ticket 21212 */ function test_strip_invalid_text( $data, $expected, $message ) { $charset = self::$_wpdb->charset; if ( isset( $data[0]['connection_charset'] ) ) { $new_charset = $data[0]['connection_charset']; unset( $data[0]['connection_charset'] ); } else { $new_charset = $data[0]['charset']; } self::$_wpdb->charset = $new_charset; self::$_wpdb->set_charset( self::$_wpdb->dbh, $new_charset ); $actual = self::$_wpdb->strip_invalid_text( $data ); self::$_wpdb->charset = $charset; self::$_wpdb->set_charset( self::$_wpdb->dbh, $charset ); $this->assertSame( $expected, $actual, $message ); }
/** * 连接数据库 * * @return \mysqli|null|resource * @throws MysqlConnectException */ public function connect() { if (is_null($this->mysql)) { $this->mysql = new \mysqli($this->host, $this->user, $this->password, $this->database, $this->port); $errno = $this->mysql->connect_errno; $error = $this->mysql->connect_error; if ($errno > 0) { $this->errno = $errno; $this->error = $error; $this->mysql->close(); throw new MysqlConnectException($error, $errno); } else { $this->mysql->query('SET NAMES UTF8'); $this->mysql->set_charset($this->charset); } return $this->mysql; } else { return $this->mysql; } }
/** * Creates the connection to the MySQL DB through the MySQLi interface * * @param boolean $useExceptions (optional) | Override on whether to use exceptions. If not passed will use _DB_USE_EXCEPTIONS * @return boolean | TRUE on success (connection was made) / FALSE on failure * @throws _Exception | If _DB_USE_EXCEPTIONS is TRUE, will throw an exception instead of returning * false on connection error */ private function createConnection($useExceptions = _DbConfig::USE_EXCEPTIONS) { $this->mysqli = new \mysqli($this->host, $this->username, $this->password, $this->dbName, $this->port, $this->socket); if (mysqli_connect_errno()) { $msg = 'Unable to connect to MySQL | ' . mysqli_connect_error(); if ($useExceptions) { _Log::fatal($msg); $_e = new _Exception($msg, 0, $e); throw $_e; } else { _Log::warn($msg); } } if (isset($this->mysqli) && $this->mysqli !== NULL) { // set charset $rc = $this->mysqli->set_charset(_DbConfig::CHARSET); if ($rc !== TRUE) { _Log::warn('Error setting character set'); } $this->isConnected = true; return TRUE; } return FALSE; }
/** * Sets the connection's character set. * * @since 3.1.0 * * @param resource $dbh The resource given by the driver * @param string $charset Optional. The character set. Default null. * @param string $collate Optional. The collation. Default null. */ public function set_charset($dbh, $charset = null, $collate = null) { if (!isset($charset)) { $charset = $this->charset; } if (!isset($collate)) { $collate = $this->collate; } if (!$dbh->set_charset($charset, $collate)) { if ($this->has_cap('collation') && !empty($charset)) { $query = $this->prepare('SET NAMES %s', $charset); if (!empty($collate)) { $query .= $this->prepare(' COLLATE %s', $collate); } $this->query($query); } } }
/** * Send/set output charset in several output media in a proper way * * @param string $mode [http|html|mysql|mysqli|pdo|text_email|html_email] * @param resource $conn The MySQL connection handler/the link identifier * * @return string header formula if there is any (in cases of html, text_email, and html_email) * @author Khaled Al-Shamaa <*****@*****.**> */ public static function header($mode = 'http', $conn = null) { $mode = strtolower($mode); $head = ''; switch ($mode) { case 'http': header('Content-Type: text/html; charset=' . $this->_outputCharset); break; case 'html': $head .= '<meta http-equiv="Content-type" content="text/html; charset='; $head .= $this->_outputCharset . '" />'; break; case 'text_email': $head .= 'MIME-Version: 1.0\\r\\nContent-type: text/plain; charset='; $head .= $this->_outputCharset . '\\r\\n'; break; case 'html_email': $head .= 'MIME-Version: 1.0\\r\\nContent-type: text/html; charset='; $head .= $this->_outputCharset . '\\r\\n'; break; case 'mysql': if ($this->_outputCharset == 'utf-8') { mysql_set_charset('utf8'); } elseif ($this->_outputCharset == 'windows-1256') { mysql_set_charset('cp1256'); } break; case 'mysqli': if ($this->_outputCharset == 'utf-8') { $conn->set_charset('utf8'); } elseif ($this->_outputCharset == 'windows-1256') { $conn->set_charset('cp1256'); } break; case 'pdo': if ($this->_outputCharset == 'utf-8') { $conn->exec('SET NAMES utf8'); } elseif ($this->_outputCharset == 'windows-1256') { $conn->exec('SET NAMES cp1256'); } break; } return $head; }