0
votes

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

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

2
dont use mysql_ its deprecated since php 5.5.0Blueblazer172
Is it a local or remote mysql server?Blinkydamo
@Blinkydamo It's a remote server.Craig Harkins
Then you may not have the right to access the remote server using root, you would need to use a normal user account with password. Usually root is restricted for security purposes.Blinkydamo
If it is remote localhost is incorrect, that is trying to access your local system.chris85

2 Answers

3
votes

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.

2
votes

you need to provide the password in define('DBPASS', 'PASSWORD');

if you are working on local machine the default password might be "root"

define('DBPASS', 'root');