Problem mysqli PHP: Access denied for user 'root' @ 'localhost' (using password: YES)

1

I'm having a problem when doing a test with a PHP code on my server. The code would be:

<?php
if(isset($_POST["id"]) && is_numeric($_POST["id"])) {
$id=$_POST["id"];
}
else {
$id=1;
}
$mysqli = new mysqli('[IP de mi servidor]', 'root', '[mi contraseña]', '[mi DB]');
if($mysqli->connect_errno) {
die("Conexión con el servidor fallida (" . $mysqli->connect_error . ").");
}
?>


There is more code later, but the error that is executed is that.

I have tried to include the IP in the user (root @ [my IP]), to restart the server, etc., and I still have the error

  

Access denied for user 'root' @ 'localhost' (using password: YES)

However, if I go from PuTTY to MySQL with the root user and the same password, I have full access.

    
asked by Juca Navaz Reque 05.03.2018 в 00:11
source

2 answers

1

The code as such is not seen to have errors, what strikes me is that in the configuration parameter you type "IP of my server" when it should be (in 99% of cases) "localhost". If that does not work, and assuming that you use cPanel, you must create a user with a password and then assign it to your database, you can try entering phpmyadmin, if you manage to login with the credentials that I told you to create, then very surely you will solve your issue.

    
answered by 05.03.2018 / 00:36
source
2

the error is telling you that you do not have access, since you are including your password and your server does not have a password, when it does not have it, it should only look like this:

$conexion = new mysqli("localhost", "root", "", "base_datos");

the quotes that I leave empty are from the password because when I do not have it, I should not write something there.

On the other hand, if you have an access password, it should look like this:

$conexion = new mysqli("localhost", "root", "clave_secreta", "base_datos");

secret_key = to the password you have assigned, check that it is well written database = name of the database, same as the name is well written

If for example the server is on your own computer then it is localhost, if it is on an external server then the IP that is assigned to you is sent and you check it from cPanel

And the order is: server, user, password and database

    
answered by 05.03.2018 в 00:35