-2

I have a straight forward script that is trying to connect to a MySQL database.

When I run the script, I get the error:

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\ShopSite\submitReg.php on line 16 Failed to connect.

The connection code is:

$server="127.0.0.1";
$db="shop";
$user="root";
$password="";

$conn=mysqli_connect($server,$user,$password,$db) or die("Failed to connect");

I can connect to phpMyAdmin and edit the database on there just fine. I am using XAMPP and running it locally.

When I go to user accounts, it says there is no password for root. I looked in the config file and there are no passwords set. I haven't set a password for anything.

I haven't went in and fiddled with settings, or created any new accounts. It was working fine (never used to get this problem), then it just started with this. I reinstalled XAMPP, still giving me this issue.

I don't get why it says using password "YES".

I've been at this for hours trying to fix it. I've looked through lots of threads. Not all necessarily apply to me, as I am running it locally, and haven't been going in and making new user accounts. Everything is default.

Before I reinstalled, I tried creating a new user account and giving it the necessary privileges. I gave it a password too. Didn't work.

I don't get why it is doing this, and am not knowledgeable enough to fix it myself. Any help is much appreciated, so that it can go back to connecting.

Matthijs
  • 2,483
  • 5
  • 22
  • 33
rross46
  • 7
  • 1
  • The fact that it says "localhost" and "using password: YES" makes me think you are not looking at the right code block. – Devon Bessemer Oct 21 '18 at 15:01
  • have you tried adding a user with a passwords and logging into the db? I would also try logging in directly with a mysqli client like heidisql – Guy Louzon Oct 21 '18 at 15:02
  • @Devon, how do you mean? – rross46 Oct 21 '18 at 15:03
  • If the code block you were looking at corresponded to the error, it would say "127.0.0.1" and "using password: NO". Are you sure the code you posted is line 16 of C:\xampp\htdocs\ShopSite\submitReg.php? – Devon Bessemer Oct 21 '18 at 15:10
  • 1
    @GuyLouzon I had tried that before and it didn't work. Tried it this time and it did. Thanks for suggesting it again. No idea why it didn't like it before. – rross46 Oct 21 '18 at 15:11
  • possible duplicate, check this out!! https://stackoverflow.com/questions/668275/cant-connect-to-mysql-with-php – dunu008 Oct 21 '18 at 15:49

2 Answers2

0

change

 $server="127.0.0.1"; 

by

$server="localhost";
SilentK1D
  • 5
  • 6
  • Verify the permission tables (reloading grants if required) on the server and that you're connecting to the correct server. – SilentK1D Oct 21 '18 at 15:08
  • Try with this : $con = mysqli_connect("localhost","root","","database_name")or die("Failed to connect"); – SilentK1D Oct 21 '18 at 15:11
0

I used XAMPP for a while and it worked quite good for me, i think the default password for your mysql server is not set to '', so i guess if you change the root password and try accessing your database using the new password, it should work.

Updating the root password

To update the root use password, you have to go by these steps:

  1. Open the XAMPP Control Panel
  2. Click on the MySQL button
  3. After the MySQL console has loaded, execute this query:

    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('pass');`

After that, change the $password variable to the new password which
is in this case pass

Adding a new user

You could also add a new user by executing this query:

GRANT ALL PRIVILEGES ON *.* TO 'new-user'@'localhost' IDENTIFIED BY 'password';

While the username is new-user and the password is password.

I hope this helps.

nomorehere
  • 332
  • 2
  • 12