Connecting to MySQL server yields error message

Hi everyone,
Trying to connect my local MySQL server I use the following code:

<?php $aa = 'localhost'; $bb = 'myname'; $cc = 'mypass'; $dd = 'mydb'; $zzz = new mysqli($aa,$bb,$cc,$dd) or die('error'); ?>

and nothing wrong occurs. Then I try to initiate parameters in an exterior generic file I name: “login.php” as follows:

<?php $aa = 'localhost'; $bb = 'myname'; $cc = 'mypass'; $dd = 'mydb'; ?>

Initial file looks now this way:

<?php require_once 'login.php'; $zzz = new mysqli($aa,$bb,$cc,$dd) or die('error'); ?>

This time,running the application I get the following error message:

Warning: mysqli::mysqli(): (HY000/1045): Access denied for user ‘myname’@‘localhost’ (using password: YES) in C:\xampp\htdocs\1.php on line 7
Can antone suggest why I can’t connect my DB host with parameters initiated in an exterior file?
Thanks !

You are also doing it wrong. You have to create the connection before you require or include your login file. Your main issue is that there could be typos or that database doesn’t exist. Make sure every spelling is correct and exactly the same. Make sure that your database exists. If you cannot connect to it using localhost, try to connect to it using the IP Address or if you are using a live server, use their specified preferences since some hosts will require you to use say mysql.sample-domain.com instead of using localhost.

1 Like

Check the values of your variables in the main code before you attempt the connection, see if they’re what you expect. Also check and double-check for typos, I’m assuming that isn’t copy-pasted from your actual application otherwise it would have kicked off about the missing $ at the start of zzz on the last line.

3 Likes

Verify the permission tables (reloading grants if required) on the server which you are connecting to the correct server.

Thank you, spaceshiptrooper ! I carefully wrote the same code again and it worked ! I have no idear what went wrong with my typing/ copy pase. Maybe interaction with diferent editors/

I belive it was copy paste from different editors. I rewrote the same code again paying much attention to do it all at same editor, checking line by line and it worked ! Thanks again !

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.