skip to Main Content

This seems to be a highly covered issue. However majority of the threads I have seen have been local connection issues, or people saying make sure the user name and password are correct, which I have done.

I am hosting my first website with a database on name cheap and have imported the database with cPanel and myPHPAdmin. However when I load my website I get a PHP error of ” SQLSTATE[28000] [1045] Access denied for user ‘cPanelUsername_dbUsername’@’server.web-hosting.com’ (using password: YES)”.
I am using this code to connect.

  private $DB;

  public function __construct() {
    $db = 'mysql:dbname=cPanelUsername_Database; charset=utf8; host=server_ip';
    $user = "cPanelUsername_DatabaseUsername";
    $password = "password";

    try {
      $this->DB = new PDO ( $db, $user, $password );
      $this->DB->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    } catch ( PDOException $e ) {
    echo ('Error establishing Connection: '.$e->getMessage());
      exit ();
    }
  }

All permissions are also granted to the user and added to the database. I have created multiple users and all result in the error.

2

Answers


  1. If there’s no error in login/password, you should ask the question to your hosting provider. Because it’s absolutely unambiguous connection error.

    Try to re-check a host. In your case the host is ‘server_ip’, but in error is ‘server.web-hosting.com’.

    Login or Signup to reply.
  2. If you are running your code from your localhost (your computer).
    Make sure you are not not putting “localhost” or “127.0.0.1” on the “host” field.

    Instead write the actual public ip address of your remote server.

    $db = 'mysql:dbname=cPanelUsername_Database; charset=utf8; host=remote_server_public_ip';
    

    Then go to CPanel and find “Remote MySQL” option that is under the “DATABASES” section and add your public ip address (public ip address of your home/office) to the list.

    Once you upload your code to your remote server then you can change the “host” field to “127.0.0.1” or “localhost”.

    I know it sounds obvious but that just gave me a hard time for about 10 mins.

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