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

14,732

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
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);

echo "Yaaay";
Share:
14,732

Related videos on Youtube

navjotdhanawat
Author by

navjotdhanawat

Updated on June 29, 2022

Comments

  • navjotdhanawat
    navjotdhanawat almost 2 years

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

    It's working on local machine but not after deploying to a server.

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

    Also Tried adding port.

    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();
        
    ?>
    
  • navjotdhanawat
    navjotdhanawat almost 8 years
    I have create new user and granted all permission.
  • navjotdhanawat
    navjotdhanawat almost 8 years
    It works with this mysql_connect("$hostname", "$username", "$password"); but not with mysqli()
  • Lucas
    Lucas almost 8 years
    You can try to change localhost to your local IP adress.
  • Lucas
    Lucas almost 8 years
    Yeah, its either local or external, not both.
  • navjotdhanawat
    navjotdhanawat almost 8 years
    Its on hosting server. I have also used ip and ip with port.

Related