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:
Solved! Go to Solution.
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.
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 🙂
@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'
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.
I've already granted my user with all privileges but I have the same error.
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