这是我的代码。
<?php
$server = "localhost";
$uname = "replace it with anything";
$pswd = "";
$conn = mysqli_connect($server, $uname, $pswd);
if(!$conn){
die('Caught');
}
else{
die('Connected');
}
?>
无论我在 mysqli_connect()
中将什么作为 username 传递。它始终返回 true
。在密码错误的情况下,它显示访问被拒绝的错误,但我不知道为什么,无论我在用户名中输入什么,它总是返回true。
最佳答案
它不返回一个 bool 值,而是一个代表连接的对象。然后您可以检查对象的连接性。来自手册:
<?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);
}
关于php - 为什么即使我传递了错误的用户名,mysqli_connect() 也返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44029563/