0

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!

  • Zach L
1
  • 2
    The error shows you are trying to connect to localhost; your code shows you are connecting to 127.0.0.1:3306. Which is it? Those are different for MySQL. First, get rid of the 3306, it should not be necessary. Next, try adding access perms for webuser from both localhost and 127.0.0.1. Commented Dec 6, 2017 at 5:20

6 Answers 6

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

also you can check MySQL user privileges, if you really have the user with that name
turns out I did have a user like that, but I removed it and nothing changes. Thanks tho :)
1

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.

Comments

1

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

1 Comment

Have tried this, does not work unfortunately :/ I have a new strange issue where when I change the password to '' it give me a new error: Error connecting: Unknown database 'learning_accounts'
1

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

3 Comments

When using mysql from the terminal it forced me to create a password so it is not blank. Strangely, when I make it blank '' it gives the error Error connecting: Unknown database 'learning_accounts'
How did you create databases and tables? Using phpMyAdmin right? If yes, when you try to login to phpMyAdmin console it'll ask you the user name and password. So, use the same user name and password in your codes too.
I created the db and tables through mac terminal using sql commands.
1

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

7 Comments

Hm, I have a password set for my root account, but removing changes the error to: Error connecting: Unknown database 'learning_accounts'
@Zacx, Check your database name. Are you sure database is created with a proper name? Are you working on localhost or server?
database name is correct, I am working on local host - I am doing this all on my macbook and from my searching it sees that macs have some issues with mysqli
Check out this accepted answer stackoverflow.com/questions/20458864/…
ah that looks promising, unfortunately I am not using MAMP, I am using XAMPP
|
-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.