此 PHP 连接本地主机 XAMPP 的代码有什么问题

What is the issue in this PHP code to connect on localhost XAMPP

错误是: 连接失败:SQLSTATE[HY000] [1045] 用户 'moomen'@'localhost' 的访问被拒绝(使用密码:是)

代码为:

<?php
$servername = "localhost";
$username = "moomen";
$password = "9124279123";
$dbname = "ecocaa";
try {
$conn = new PDO("mysql:host=$servername;dbname=$servername", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";

} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}

我发现你的代码有问题 在您使用的服务器名称的 dbname 中,更改它将起作用的代码。