Friday, August 17, 2018

php - 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.


Answer



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.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...