这是我的代码。
F13
无论我将<?php
$server = "localhost";
$uname = "replace it with anything";
$pswd = "";
$conn = mysqli_connect($server, $uname, $pswd);
if(!$conn){
die('Caught');
}
else{
die('Connected');
}
?>
传递给用户名。它总是返回mysqli_connect()
。如果密码错误,则显示访问被拒绝的错误,但我不知道为什么,无论我在用户名中输入什么,它都会返回true。
答案 0 :(得分:7)
它不返回布尔值,而是返回表示连接的对象。然后,您可以检查对象的连接性。从手册:
<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
答案 1 :(得分:1)
尝试
<?php
$mysqli = new mysqli("host", "username", "password", "database") or die($mysqli->error());
if ($mysqli->connect_errno) {
echo "error";
exit();
}
?>