我无法理解为什么这个简单的代码片段会这样。 这只是一个连接到 MariaDB 的基本 config.php 脚本。 当我输入正确的密码时,我会得到预期的行为,并且可以看到“连接成功”,但是当我出于测试目的故意输入错误的密码时,我不会收到错误消息。当我执行脚本时,我什至没有得到第二个 echo 语句“hi Again”。它似乎被困在 $conn 分配上。为什么?
PHP 版本 8.2.7
MariaDB 版本 10.11.4-MariaDB-1~deb12u1 - Debian 12
echo "hi";
$servername = "localhost";
$username = "jose";
$password = "9750";
$database = "miBD";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
echo "hi again";
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
请开启调试,参见:如何在php中正确调试
D:\TEMP>php hi.php
hiPHP Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'jose'@'localhost' (using password: YES) in D:\TEMP\hi.php:11
Stack trace:
#0 D:\TEMP\hi.php(11): mysqli_connect()
#1 {main}
thrown in D:\TEMP\hi.php on line 11
Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'jose'@'localhost' (using password: YES) in D:\TEMP\hi.php on line 11
mysqli_sql_exception: Access denied for user 'jose'@'localhost' (using password: YES) in D:\TEMP\hi.php on line 11
Call Stack:
0.0002 405416 1. {main}() D:\TEMP\hi.php:0
0.0003 405448 2. mysqli_connect($hostname = 'localhost', $username = 'jose', $password = '[Sensitive Parameter]', $database = 'miBD') D:\TEMP\hi.php:11