本文整理汇总了PHP中mysqli_init函数的典型用法代码示例。如果您正苦于以下问题:PHP mysqli_init函数的具体用法?PHP mysqli_init怎么用?PHP mysqli_init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mysqli_init函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
private function __construct()
{
$this->mysql = mysqli_init();
$conf = Vera_Conf::getConf('database');
$this->_connect($conf);
$this->mysql->set_charset('utf8');
}
示例2: PMA_DBI_connect
/**
* connects to the database server
*
* @uses $GLOBALS['cfg']['Server']
* @uses PMA_auth_fails()
* @uses PMA_DBI_postConnect()
* @uses MYSQLI_CLIENT_COMPRESS
* @uses MYSQLI_OPT_LOCAL_INFILE
* @uses strtolower()
* @uses mysqli_init()
* @uses mysqli_options()
* @uses mysqli_real_connect()
* @uses defined()
* @param string $user mysql user name
* @param string $password mysql user password
* @param boolean $is_controluser
* @return mixed false on error or a mysqli object on success
*/
function PMA_DBI_connect($user, $password, $is_controluser = false)
{
$server_port = empty($GLOBALS['cfg']['Server']['port']) ? false : (int) $GLOBALS['cfg']['Server']['port'];
if (strtolower($GLOBALS['cfg']['Server']['connect_type']) == 'tcp') {
$GLOBALS['cfg']['Server']['socket'] = '';
}
// NULL enables connection to the default socket
$server_socket = empty($GLOBALS['cfg']['Server']['socket']) ? null : $GLOBALS['cfg']['Server']['socket'];
$link = mysqli_init();
mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, true);
$client_flags = 0;
/* Optionally compress connection */
if ($GLOBALS['cfg']['Server']['compress'] && defined('MYSQLI_CLIENT_COMPRESS')) {
$client_flags |= MYSQLI_CLIENT_COMPRESS;
}
/* Optionally enable SSL */
if ($GLOBALS['cfg']['Server']['ssl'] && defined('MYSQLI_CLIENT_SSL')) {
$client_flags |= MYSQLI_CLIENT_SSL;
}
$return_value = @mysqli_real_connect($link, $GLOBALS['cfg']['Server']['host'], $user, $password, false, $server_port, $server_socket, $client_flags);
// Retry with empty password if we're allowed to
if ($return_value == false && isset($cfg['Server']['nopassword']) && $cfg['Server']['nopassword'] && !$is_controluser) {
$return_value = @mysqli_real_connect($link, $GLOBALS['cfg']['Server']['host'], $user, '', false, $server_port, $server_socket, $client_flags);
}
if ($return_value == false) {
PMA_auth_fails();
}
// end if
PMA_DBI_postConnect($link, $is_controluser);
return $link;
}
示例3: sha_connect
function sha_connect($offset, $host, $db, $port, $socket, $file)
{
$link = mysqli_init();
if (!$link->options(MYSQLI_SERVER_PUBLIC_KEY, $file)) {
printf("[%03d + 001] mysqli_options failed, [%d] %s\n", $offset, $link->errno, $link->error);
return false;
}
if (!$link->real_connect($host, 'shatest', 'shatest', $db, $port, $socket)) {
printf("[%03d + 002] [%d] %s\n", $offset, $link->connect_errno, $link->connect_error);
return false;
}
if (!($res = $link->query("SELECT id FROM test WHERE id = 1"))) {
printf("[%03d + 003] [%d] %s\n", $offset, $link->errno, $link->error);
}
return false;
if (!($row = mysqli_fetch_assoc($res))) {
printf("[%03d + 004] [%d] %s\n", $offset, $link->errno, $link->error);
return false;
}
if ($row['id'] != 1) {
printf("[%03d + 005] Expecting 1 got %s/'%s'", $offset, gettype($row['id']), $row['id']);
return false;
}
$res->close();
$link->close();
return true;
}
示例4: db_connect
/**
* Initialize database connection(s)
*
* Connects to the specified master database server, and also to the slave server if it is specified
*
* @param string Name of the database server - should be either 'localhost' or an IP address
* @param integer Port of the database server - usually 3306
* @param string Username to connect to the database server
* @param string Password associated with the username for the database server
* @param string Persistent Connections - Not supported with MySQLi
* @param string Configuration file from config.php.ini (my.ini / my.cnf)
* @param string Mysqli Connection Charset PHP 5.1.0+ or 5.0.5+ / MySQL 4.1.13+ or MySQL 5.1.10+ Only
*
* @return object Mysqli Resource
*/
function db_connect($servername, $port, $username, $password, $usepconnect, $configfile = '', $charset = '')
{
set_error_handler(array($this, 'catch_db_error'));
$link = mysqli_init();
# Set Options Connection Options
if (!empty($configfile)) {
mysqli_options($link, MYSQLI_READ_DEFAULT_FILE, $configfile);
}
try {
// this will execute at most 5 times, see catch_db_error()
do {
$connect = $this->functions['connect']($link, $servername, $username, $password, '', $port);
} while ($connect == false and $this->reporterror);
} catch (Exception $e) {
restore_error_handler();
throw $e;
}
restore_error_handler();
if (!empty($charset)) {
if (function_exists('mysqli_set_charset')) {
mysqli_set_charset($link, $charset);
} else {
$this->sql = "SET NAMES {$charset}";
$this->execute_query(true, $link);
}
}
return !$connect ? false : $link;
}
示例5: db_Connect
function db_Connect($host = CMW_DB_HOST, $login = CMW_DB_LOGIN, $password = CMW_DB_PASSWORD, $name = CMW_DB_NAME, $port = _INI_MYSQLI_DEFAULT_PORT, $socket = _INI_MYSQLI_DEFAULT_SOCKET)
{
// Safely call this multiple times, only the first time has any effect //
if (!db_IsConnected()) {
global $db;
$db = mysqli_init();
//mysqli_options($db, ...);
if (defined('CMW_DB_PORT')) {
$port = CMW_DB_PORT;
}
if (defined('CMW_DB_SOCKET')) {
$socket = CMW_DB_SOCKET;
}
$flags = null;
// Connect to the database //
mysqli_real_connect($db, $host, $login, $password, $name, $port, $socket, $flags);
// http://php.net/manual/en/mysqli.quickstart.connections.php
if ($db->connect_errno) {
db_Log("Failed to connect: (" . $db->connect_errno . ") " . $db->connect_error);
}
// Set character set to utf8mb4 mode (default is utf8mb3 (utf8). mb4 is required for Emoji)
mysqli_set_charset($db, 'utf8mb4');
// More info: http://stackoverflow.com/questions/279170/utf-8-all-the-way-through
}
}
示例6: connect
/**
* Установка соединения
*
* @return bool
*
* @throws \Cms\Db\Engine\Driver\Exception\ConnectionException
*/
public function connect()
{
$resource = null;
$this->resource = null;
$this->connected = false;
$host = $this->host;
if ($this->isPersistent()) {
$host = 'p:' . $host;
}
try {
$resource = mysqli_init();
$resource->set_opt(MYSQLI_OPT_CONNECT_TIMEOUT, $this->timeout);
$resource->real_connect($host, $this->user, $this->password, $this->database, intval($this->port));
} catch (\Exception $e) {
$msg = sprintf('Не удалось установить соединение с базой данных "%s": %s', $this->database, $e->getMessage());
throw new ConnectionException($msg, $e->getCode());
}
if ($resource->connect_errno) {
$msg = sprintf('Не удалось установить соединение с базой данных "%s": %s', $this->database, $resource->connect_error);
throw new ConnectionException($msg, $resource->connect_errno);
}
$this->resource = $resource;
$this->connected = true;
$this->executeConnectQueries();
return true;
}
示例7: __construct
/**
* @param object $link
* @param array $options
*/
public function __construct($link = false, $options = array())
{
foreach ($options as $key => $value) {
$this->{$key} = $value;
}
if ($link) {
$this->link = $link;
} else {
if (!empty($options)) {
if (isset($this->host)) {
$this->hostname = $this->host;
}
if (isset($this->dbname)) {
$this->database = $this->dbname;
}
//$this->link = @mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
$this->link = mysqli_init();
mysqli_options($this->link, MYSQLI_OPT_CONNECT_TIMEOUT, 1);
if (@mysqli_real_connect($this->link, $this->hostname, $this->username, $this->password, $this->database)) {
$names = "SET NAMES '" . $this->charset . "';";
mysqli_query($this->link, $names);
$charset = "SET CHARSET '" . $this->charset . "';";
mysqli_query($this->link, $charset);
} else {
$this->link = false;
}
}
}
}
示例8: _connect
protected function _connect()
{
//try {
$this->_config = App::$_registry["db"];
//pr($this->_config);exit;
// if connected return
if ($this->_connection) {
return;
}
if (isset($this->_config['port'])) {
$port = (int) $this->_config['port'];
} else {
$port = null;
}
$this->_connection = mysqli_init();
$_isConnected = @mysqli_real_connect($this->_connection, $this->_config['host'], $this->_config['username'], $this->_config['password'], $this->_config['dbname'], $port, null, MYSQLI_CLIENT_FOUND_ROWS);
if ($_isConnected === false || mysqli_connect_errno()) {
$this->closeConnection();
//throw new \Exception(mysqli_connect_error());
show_error('DB Error', mysqli_connect_error());
}
//}
/* catch (\Exception $e) {
echo $e->getTraceAsString();
} */
}
示例9: __construct
public function __construct($dsn)
{
$base = preg_replace('{^/}s', '', $dsn['path']);
if (!class_exists('mysqli')) {
return $this->_setLastError('-1', 'mysqli extension is not loaded', 'mysqli');
}
try {
$this->link = mysqli_init();
if (!$this->link) {
$this->_setLastError(-1, 'mysqli_init failed', 'new mysqli');
} else {
if (!$this->link->options(MYSQLI_OPT_CONNECT_TIMEOUT, isset($dsn['timeout']) && $dsn['timeout'] ? $dsn['timeout'] : 0)) {
$this->_setLastError($this->link->connect_errno, $this->link->connect_error, 'new mysqli');
} else {
if (!$this->link->real_connect(isset($dsn['persist']) && $dsn['persist'] ? 'p:' . $dsn['host'] : $dsn['host'], $dsn['user'], isset($dsn['pass']) ? $dsn['pass'] : '', $base, empty($dsn['port']) ? NULL : $dsn['port'], NULL, isset($dsn['compression']) && $dsn['compression'] ? MYSQLI_CLIENT_COMPRESS : NULL)) {
$this->_setLastError($this->link->connect_errno, $this->link->connect_error, 'new mysqli');
} else {
if (!$this->link->set_charset(isset($dsn['enc']) ? $dsn['enc'] : 'UTF8')) {
$this->_setLastError($this->link->connect_errno, $this->link->connect_error, 'new mysqli');
} else {
$this->isMySQLnd = method_exists('mysqli_result', 'fetch_all');
}
}
}
}
$this->isMySQLnd = method_exists('mysqli_result', 'fetch_all');
} catch (mysqli_sql_exception $e) {
$this->_setLastError($e->getCode(), $e->getMessage(), 'new mysqli');
}
}
示例10: connect
/**
* Connect to the database
*
* @return bool false on failure / mysqli MySQLi object instance on success
*/
public function connect()
{
// Try and connect to the database
// if (!isset(self::$connection)) {
// Load configuration as an array. Use the actual location of your configuration file
// self::$connection = new mysqli('localhost', 'root', 'root', 'servicioslegales');
// }
self::$connection = mysqli_init();
if (!self::$connection) {
die('mysqli_init failed');
}
if (!self::$connection->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
die('Setting MYSQLI_INIT_COMMAND failed');
}
if (!self::$connection->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}
if (!self::$connection->real_connect('localhost', 'root', 'root', 'servicioslegales')) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
// If connection was not successful, handle the error
if (self::$connection === false) {
// Handle error - notify administrator, log to a file, show an error screen, etc.
return false;
}
return self::$connection;
}
示例11: doConnect
/**
* 连接数据库
*
* @param array $config
* @return string 返回连接的id
*/
public function doConnect(array &$config)
{
try {
if (empty($persistent)) {
$resource = \mysqli_init();
\mysqli_options($resource, \MYSQLI_OPT_CONNECT_TIMEOUT, 3);
if (isset($this->config['option']) && is_array($this->config['option'])) {
foreach ($this->config['option'] as $k => $v) {
\mysqli_options($resource, $k, $v);
}
}
\mysqli_real_connect($resource, $config['hostname'], $config['username'], $config['password'], $config['database'], $config['port'], null, \MYSQLI_CLIENT_COMPRESS);
} else {
$resource = new \mysqli($config['hostname'], $config['username'], $config['password'], $config['database'], $config['port']);
}
# 设置语言
$resource->set_charset($this->config['charset']);
return $resource;
} catch (Exception $e) {
if (2 === $e->getCode() && preg_match('#(Unknown database|Access denied for user)#i', $e->getMessage(), $m)) {
# 指定的库不存在,直接返回
$lastError = strtolower($m[1]) === 'unknown database' ? __('The mysql database does not exist') : __('The mysql database account or password error');
} else {
$lastError = $e->getMessage();
}
$lastErrorCode = $e->getCode();
}
throw new Exception($lastError, $lastErrorCode);
}
示例12: __construct
protected function __construct()
{
$this->mysqli = mysqli_init();
if (!$this->mysqli->real_connect(Configuration::read('db_host'), Configuration::read('db_username'), Configuration::read('db_password'), Configuration::read('db_name'))) {
throw new MysqliConnectionException('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
}
示例13: connect
/**
* Database connection
*
* @param array $conf
* @return bool|resource
*/
private function connect()
{
if ($this->driver == 'ext/mysqli') {
$link = mysqli_init();
if (!$link) {
$link = false;
} else {
$link = @mysqli_connect($this->host, $this->user, $this->password);
if ($link !== false) {
$db = @mysqli_select_db($link, $this->db);
mysqli_set_charset($link, "utf8");
}
}
if ($link === false) {
$this->error('MySQL connection error: ' . mysqli_connect_error());
}
} else {
$link = @mysql_connect($this->host, $this->user, $this->password);
if ($link !== false) {
$db = @mysql_select_db($this->db, $link);
mysql_query("SET NAMES 'utf8'");
} else {
if ($link === false) {
$this->error('MySQL connection error: ' . mysql_error());
}
}
}
return $link;
}
示例14: connect
/**
* Connect to a database and log in as the specified user.
*
* @param string $dsn the data source name (see DB::parseDSN for syntax)
* @param boolean $persistent (optional) whether the connection should
* be persistent
* @return mixed DB_OK on success, a DB error on failure
* @access public
*/
function connect($dsninfo, $persistent = false)
{
if (!DB::assertExtension('mysqli')) {
return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
}
$this->dsn = $dsninfo;
$conn = false;
@ini_set('track_errors', true);
if ($this->getOption('ssl') === true) {
$init = mysqli_init();
mysqli_ssl_set($init, empty($dsninfo['key']) ? null : $dsninfo['key'], empty($dsninfo['cert']) ? null : $dsninfo['cert'], empty($dsninfo['ca']) ? null : $dsninfo['ca'], empty($dsninfo['capath']) ? null : $dsninfo['capath'], empty($dsninfo['cipher']) ? null : $dsninfo['cipher']);
if ($conn = @mysqli_real_connect($init, $dsninfo['hostspec'], $dsninfo['username'], $dsninfo['password'], $dsninfo['database'], $dsninfo['port'], $dsninfo['socket'])) {
$conn = $init;
}
} else {
$conn = @mysqli_connect($dsninfo['hostspec'], $dsninfo['username'], $dsninfo['password'], $dsninfo['database'], $dsninfo['port'], $dsninfo['socket']);
}
@ini_restore('track_errors');
if (!$conn) {
if (($err = @mysqli_connect_error()) != '') {
return $this->raiseError(DB_ERROR_CONNECT_FAILED, null, null, null, $err);
} elseif (empty($php_errormsg)) {
return $this->raiseError(DB_ERROR_CONNECT_FAILED);
} else {
return $this->raiseError(DB_ERROR_CONNECT_FAILED, null, null, null, $php_errormsg);
}
}
if ($dsninfo['database']) {
$this->_db = $dsninfo['database'];
}
$this->connection = $conn;
return DB_OK;
}
示例15: open
/**
* Opens the database connection.
*
* @return bool Boolean true on success, throws a RuntimeException otherwise
*/
public function open()
{
if (is_object($this->link)) {
return true;
}
if ($this->ssl == true) {
if (empty($this->clientcert)) {
throw new \RuntimeException("clientcert not defined");
}
if (empty($this->clientkey)) {
throw new \RuntimeException("clientkey not defined");
}
if (empty($this->cacert)) {
throw new \RuntimeException("cacert not defined");
}
$this->link = mysqli_init();
$this->link->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
$this->link->ssl_set($this->clientkey, $this->clientcert, $this->cacert, NULL, NULL);
$this->link->real_connect($this->host, $this->username, $this->password, $this->database, $this->port, $this->socket, MYSQLI_CLIENT_SSL);
} else {
$this->link = mysqli_connect($this->host, $this->username, $this->password, $this->database, $this->port, $this->socket);
}
if (mysqli_connect_errno()) {
throw new \RuntimeException(sprintf('could not connect to %s : (%d) %s', $this->database, mysqli_connect_errno(), mysqli_connect_error()));
}
if (!mysqli_set_charset($this->link, $this->charset)) {
throw new \RuntimeException(sprintf('error loading character set %s : (%d) %s', $this->charset, mysqli_errno($this->link), mysqli_error($this->link)));
}
return true;
}