当前位置: 首页 > 知识库问答 >
问题:

连接失败:拒绝用户'username'@'localhost'的访问(使用密码:YES)

白子明
2023-03-14

警告:mysqli::mysqli():(HY000/1045):拒绝访问/home/domainname/public_html/autopublish/test.php中的用户'usernam'@'localhost'(使用密码:YES)。第8行连接失败:拒绝访问用户'usernam'@'localhost'(使用密码:YES)

<?php
    $servername = "localhost";
    $username = "'xxxxx";
    $password = "xxxxx";
    $dbname = "xxxxx";

    // Create connection
    $conn = new mysqli($servername, $username, $password);  **//line 8**
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } else {
        echo "no connection";
    }

    $sql = "SELECT * FROM pages";
    $result = $conn->query($sql);
    
    $data = array();
    $arr = array();
    
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            
        }
    } else {
        echo "No Token";
    }
    print_r(json_encode($data));
    $conn->close();
    
?>

共有1个答案

张姚石
2023-03-14

用户没有数据库所需的权限,或者密码错误。

否则,移除用户并使用以下语句创建:

1.CREATE USER 'user'@'localhost' IDENTIFIED BY '123456789';
2.
3.GRANT ALL PRIVILEGES ON project.* TO 'user'@'localhost' WITH GRANT OPTION;

或者试试这个:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
//Create
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);

echo "Yaaay";
 类似资料: