Craig Harkins Craig Harkins - 9 months ago 43
MySQL Question

mysql_connect(): Access denied for user 'root'@'localhost'

For school I have to build and basic website using PHP and mysql. The websites function is to allow a user to register and then sign in and out. To do this, since I have no prior knowledge of PHP I was trying to follow this tutorial. I got sever space with godaddy and using mysql i created the database called details_db. I created the files the tutorial told me and uploaded to the server. Now when I go onto the website I get these errors.

Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: NO) in /home/pendrive34/public_html/dbconnect.php on line 12

Warning: mysql_select_db(): Access denied for user ''@'localhost' (using password: NO) in /home/pendrive34/public_html/dbconnect.php on line 13

Warning: mysql_select_db(): A link to the server could not be established in /home/pendrive34/public_html/dbconnect.php on line 13
Connection failed : Access denied for user ''@'localhost' (using password: NO)

This is the code in the file deconnect.php


// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE );
// but I strongly suggest you to use PDO or MySQLi.

define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'details_db');

$conn = mysql_connect(DBHOST,DBUSER,DBPASS);
$dbcon = mysql_select_db(DBNAME);

if ( !$conn ) {
die("Connection failed : " . mysql_error());

if ( !$dbcon ) {
die("Database Connection failed : " . mysql_error());

I've been trying to find the answer to the problem for a while but my limited knowledge is holding me back. If anyone could help me it would be greatly appreciated.

Answer Source

You have to write mysql root users password to

define('DBPASS', '');

this line like..

define('DBPASS', 'password_of_root_user');

If you are trying to connect a database on remote machine (like a shared hosting), you have to change DBHOST, DBUSER and DBPASS to values that given you by hosting company.