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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Zacx
  • 420
  • 5
  • 10
  • 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`. – Don't Panic Dec 06 '17 at 05:20

6 Answers6

1

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

Joel Wembo
  • 814
  • 6
  • 10
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.

rubyshine72
  • 132
  • 1
  • 6
1

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

  • 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'``` – Zacx Dec 06 '17 at 21:12
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
Shashanth
  • 4,995
  • 7
  • 41
  • 51
  • 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'``` – Zacx Dec 06 '17 at 21:14
  • 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. – Shashanth Dec 07 '17 at 04:38
  • I created the db and tables through mac terminal using sql commands. – Zacx Dec 07 '17 at 04:44
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

Naren Verma
  • 2,205
  • 5
  • 38
  • 95
  • Hm, I have a password set for my root account, but removing changes the error to: ```Error connecting: Unknown database 'learning_accounts'``` – Zacx Dec 06 '17 at 17:35
  • @Zacx, Check your database name. Are you sure database is created with a proper name? Are you working on localhost or server? – Naren Verma Dec 07 '17 at 05:15
  • 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 – Zacx Dec 07 '17 at 05:29
  • Check out this accepted answer https://stackoverflow.com/questions/20458864/connecting-to-mysqli-through-terminal-in-mac-os-using-mamp – Naren Verma Dec 07 '17 at 05:41
  • ah that looks promising, unfortunately I am not using MAMP, I am using XAMPP – Zacx Dec 07 '17 at 05:43
  • Have you checked your xampp path? It should be /Applications/XAMPP/bin/ – Naren Verma Dec 07 '17 at 05:47
  • My xampp path looks normal, been at this for 3 days now I think I've tried every solution I can find on the internet. Last resort is going to be reinstalling everything. – Zacx Dec 07 '17 at 17:50
0

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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Zacx
  • 420
  • 5
  • 10