skip to Main Content

So, I’m SUPER new to mySQL. Having issues connecting to my database with PHP. Hoping someone can point me in the right direction.

I can login to our Cpanel using my username/password. Using the web gui I was able to create a database.

Now when I try to connect to the database (or even just the server for that matter) using PHP I get an error:

Warning: mysqli::__construct(): (HY000/1045): Access denied for user ‘username’@’servername’ (using password: YES) in /home/ropepart/public_html/techportal/sandbox/mysqltest.php on line 8
Connection failed: Access denied for user ‘username’@’servername’ (using password: YES)

To me, it seems like this is a username/password error. But I an using the same username/password that I use to login to the web gui. Why can I successfully login there, but can’t login with PHP?

Here’s my code (taken pretty much directly from W3Schools):

<?php
$servername = "servername";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

(Hopefully it’s obvious that I’ve changed my servername/username/password for the purposes of this post)

3

Answers


  1. Check the details correctly, By deafult
    host = localhost
    username = root
    password = (No password leave it as empty)
    database = (your database)

    <?php 
    
    $connection = new mysqli('host','username','password','your_database');
    
    if($connection->connect_error || $connection->error){
    
    echo "error";
    
    }else{
    
    echo "done";
    
    }
    
    ?>
    
    Login or Signup to reply.
  2. you should add database name.

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

    //if you are using xampp you shoul locate this file you get information from this
    passwords.txt

    Login or Signup to reply.
  3. MySQL database username/password is not necessarily same as cPanel username & password.

    Here is what you should do:

    1. Login to your cPanel
    2. From dashboard click “MySQL® Databases”
    3. Add a new user from there (https://prnt.sc/kpiby9). Just fill username
      and password (note down the password).
    4. Add that user with your database by selecting both from the dropdown
      (https://prnt.sc/kpidqx)
    5. Now, use this username & password to connect with the database.

    mysqli_connect(serverhostname,username,password,dbname);

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search