cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Go to solution

access denied connecting to mysql database from php

I have a Linux Hosting account.

 

I have created a MySQL database using the 'MySQL Databases' function in CPanel. Using the same page, I created a user and added it to the new database. So far so good.

 

My first attempt to use PHP to connect to this database uses this code:

 

$servername = "localhost";
$username = "fakeuser";
$password = "fakepassword";
$database = "fakedb";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);
echo $conn->connect_error;
 
When I run this script, I get the following error:
Access denied for user 'fakeuser'@'localhost' to database 'fakedb'
 
In PHPMyAdmin I have been able to create tables. I cannot grant privileges to the user I created with the MySQL Databases tool though. If I run a GRANT ALL PRIVILEGES... SQL command, I get a different 'access denied for user <some user ID I never heard of!>'.
 
Any ideas?
 
Thanks in anticipation
5 REPLIES 5
Resolver II

Re: access denied connecting to mysql database from php

You can try it in 2 ways:

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";

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

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

or this one:

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";

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

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

Method 3 — Connecting to the database using mysqli_connect (Procedural)
<?php

$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

echo "Connected successfully";
?>

If it doesn't work please share a screenshot of the error Smiley Happy

Re: access denied connecting to mysql database from php

@premtonxhafa thanks for trying to help, much appreciated.

 

I don't think this is a PHP coding problem.

 

Nevertheless, I tested both of your approaches and both produced the same error.

 

A screenshot won't tell you any more than I already provided. You get a single error message like this one (user and db name changed):

 

Connection failed: Access denied for user 'xxxx'@'localhost' to database 'yyyy'

Highlighted
Solution

Re: access denied connecting to mysql database from php

Problem solved (thanks for GoDaddy support).

 

The problem was due to insufficient / missing MySQL privileges.

 

I'd tried to GRANT PRIVILEGES to my user from within PhpMyAdmin but did not have the privileges required to... grant privileges.  However it turns out you can select privileges from within the CPanel MySQL Databases page. Under the Current Databases section, there's a list of databases you've created and the users assigned to each database. What I hadn't noticed was that you can click on the user name(s) and this brings up a different page with checkboxes next to the usual MySQL privileges (e.g. ALTER, CREATE, SELECT etc). After I'd enabled my required privileges I was able to access my database.

Re: access denied connecting to mysql database from php

I've already granted my user with all privileges but I have the same error.

New

Re: access denied connecting to mysql database from php

Same situation, created SQL user with all privileges, added them to correct database, have the correct user & password in my script, still getting this error