Zacx
Zacx

Reputation: 438

Access Denied Connecting to MySQL with PHP

I know there are a lot of threads asking this question already, but I have been at this for hours and am at wits end. I am attempting to connect to a MySQL database that I am running of my MacBook with the following PHP:

<?php
DEFINE('DB_USER', 'webuser');
DEFINE('DB_PASSWORD', 'thispassword');
DEFINE('DB_HOST', '127.0.0.1:3306');
DEFINE('DB_NAME', 'learning_accounts');

$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die('Error connecting: ' .
mysqli_connect_error()
);

?>

That returns the following error:

Error connecting: Access denied for user 'webuser'@'localhost' (using password: YES)

I can connect fine through the terminal, even trying to connect with PHP and root does not work. I am new to both PHP and MySQL so any guidance and insight would be appreciated!

I have tried tons of solutions such as granting privileges, flushing privileges, creating new users, using PDO, nothing has been working - please help!

Thanks so much, I hate learning new languages because then I'm a lost fish when it comes to debugging at first!

Update

I have a password set for my root account, but changing the DB_PASSWORD variable to '' changes the error to: Error connecting: Unknown database 'learning_accounts'

Update 2 I have fixed the issue, check my answer to see how, if you are having this problem then I hope you found this thread quickly!

Upvotes: 0

Views: 5072

Answers (6)

Zacx
Zacx

Reputation: 438

The issue was that I had installed php separately, and made my database, then installed XAMPP and tried to connect to that database. The proper way is to create the database using php my admin, which comes with XAMPP, and has it's own php, MySQL, and apache that you must use for everything to work.

Upvotes: -1

Naren Verma
Naren Verma

Reputation: 2307

If you are working on the local server then no need to require the password just make it `blank'.

    define('DB_HOST', 'localhost');    
    define('DB_USER', 'root');
    define('DB_PASSWORD', '');
    define('DB_NAME', 'learning_accounts');

$conn = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
if (!$link) {
    die("Database connection failed: " . mysqli_error());
}

If you are working on the server the just change the DB_USER, DB_PASSWORD and DB_NAME. If still not able to connect then check the database name is correct or not.

If everything is perfect you have to check the below link

How to install MySQLi

Upvotes: 1

Shashanth
Shashanth

Reputation: 5180

As @DontPanic suggested in his comment get rid off the 3306 from 127.0.0.1:3306.

If you're using XAMPP, WAMP or EasyPHP the MySQL password isn't set by-default. Hence you should use

DEFINE('DB_USER', 'root'); // default user name
DEFINE('DB_PASSWORD', ''); // empty password just open single quote and close

Upvotes: 1

James Ezeh
James Ezeh

Reputation: 5

Try changing "DB_HOST" to "localhost". If it did no solve the problem, then reinstall your mysql server

Upvotes: 1

rubyshine72
rubyshine72

Reputation: 132

if you tried privilleges, and so on tons of solutions, then you may make another account, and try it.

If same problem occurs, then it is mysql service/daemon problem.

I have same experience before time.

So at that time, I reinstalled mysql server, and problem was fixed.

Upvotes: 1

Joel Wembo
Joel Wembo

Reputation: 870

You probably have an anonymous user ''@'localhost' or ''@'127.0.0.1'

Upvotes: 1

Related Questions