mysqli::init
mysqli_init
(PHP 5, PHP 7, PHP 8)
mysqli::init -- mysqli_init — 初始化 MySQLi 并返回用于 mysqli_real_connect() 的对象
说明
面向对象风格
过程化风格
分配或者初始化适合 mysqli_options() 和 mysqli_real_connect() 的 MYSQL 对象。
注意:
在调用 mysqli_real_connect() 之前,对其它 mysqli 函数(mysqli_options() 和 mysqli_ssl_set() 除外)的后续调用都会失败。
参数
此函数没有参数。
更新日志
版本 | 说明 |
---|---|
8.1.0 | 弃用面向对象风格的 mysqli::init() 方法。用 parent::__construct() 替换对 parent::init() 的调用。 |
示例
参见
- mysqli_options() - 设置选项
- mysqli_close() - 关闭先前打开的数据库连接
- mysqli_real_connect() - 建立一个 MySQL 服务器连接
- mysqli_connect() - 别名 mysqli::__construct
+添加备注
用户贡献的备注 2 notes
Kam.Dab ¶
1 year ago
I wrote support ssl mysqli you don't need change anymore mysqli connect exchange to your own mysqli . Overwrite __construct mysqli with support ssl can be like that:
<?php class myssl_mysqli extends \mysqli {
public function __construct($db_host, $db_user, $db_pass, $db_name, $port, $persistent = true, $ssl = false, $certpublic = "") {
if($ssl) {
parent::init();
parent::options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, false);
parent::ssl_set(NULL, NULL, $certpublic, NULL, NULL);
parent::real_connect(($persistent ? 'p:' : '') . $db_host, $db_user, $db_pass, $db_name, $port, '', MYSQLI_CLIENT_SSL | MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT );
} else {
parent::__construct($db_host, $db_user, $db_pass, $db_name, $port);
}
}
$db = new myssl_mysqli('localhost','user', 'pass','db', '3306', true, true, '/home/mypublicowncert.pem');
?>
in this example i off the verificate cert by authority ssl, due it own cery created
evgen at sysmasters dot net ¶
3 years ago
Correct way to connect db
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("127.0.0.1", "db_user", "db_pass", "db_name",3306);
$result = $mysqli->query("SELECT somefield1, somefield2 FROM sometable ORDER BY ID LIMIT 3");
/* Close the connection as soon as it becomes unnecessary */
$mysqli->close();
foreach ($result as $row) {
/* Processing data received from the database */
echo var_dump ($row);
}