Connection failed: Access denied for user ''username'@'localhost' (using password: YES)

喜欢而已 提交于 2019-12-13 15:46:44

问题


I am trying to connect to mysql on server but gives following error. User is created and granted all privileges.

Its working on local machine but not after deploying to server.

Also mysql_connect function works but mysqli() gives access denied as mentioned below.

Also Tried adding port.

Any Help please.

Help will be appreciated. Thanks,

Warning: mysqli::mysqli(): (HY000/1045): Access denied for user ''usernam'@'localhost' (using password: YES) in /home/domainname/public_html/autopublish/test.php on line 8 Connection failed: Access denied for user ''username'@'localhost' (using password: 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:


Either the user does not have the required rights for the database or the password is wrong.

Otherwise remove the user and use the following statement to create:

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

or try this:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
//Create
$conn = new mysqli($servername, $username, $password, $dbname);
//Check
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Yaaay";
?>


来源:https://stackoverflow.com/questions/37504065/connection-failed-access-denied-for-user-usernamelocalhost-using-passwor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!