Problem connecting to MySQL from PHP (Resolved) [closed]

0

(RESOLVED: I had the password wrong) I get this error when connecting: (I have omitted user and pass for obvious reasons)

  

Access denied for user 'test'@'127.0.0.1' (using password: YES)

<?php
$mysqli = new mysqli("127.0.0.1", "test", "password");

/* comprobar la conexión */
if ($mysqli->connect_errno) {
    printf("Conexión fallida: %s\n", $mysqli->connect_error);
    exit();
}

/* comprobar si el servidor sigue vivo */
if ($mysqli->ping()) {
    printf ("¡La conexión está bien!\n");
} else {
    printf ("Error: %s\n", $mysqli->error);
}

/* cerrar la conexión */
$mysqli->close();
?>
    
asked by Alejandro 27.12.2017 в 13:02
source

2 answers

1

The username and password you have entered are correct? Normally the user is root, and the password is blank unless you changed them.

Try this to see

    <?php
    // Ejemplo de conexión a base de datos MySQL con PHP.
    //


    // Datos de la base de datos
    $usuario = "root";
    $password = "";
    $servidor = "localhost";
    $basededatos = "alumnos";

    // creación de la conexión a la base de datos con mysql_connect()
    $conexion = mysqli_connect( $servidor, $usuario, "" ) or die ("No se ha podido conectar al servidor de Base de datos");

    // Selección del a base de datos a utilizar
    $db = mysqli_select_db( $conexion, $basededatos ) or die ( "Upps! Pues va a ser que no se ha podido conectar a la base de datos" );

mysqli_close( $conexion );
?>
    
answered by 27.12.2017 в 13:21
1

If the user and the password are fine then the error as commented @Marc is permissions, when you install a bd engine this is installed with local permissions, so you must create the user either from your database administrator or in the following way:

#Crear Usuario
CREATE USER 'nombre_usuario'@'localhost' IDENTIFIED BY 'tu_contrasena';

#Asignar Privilegios
GRANT ALL PRIVILEGES ON * . * TO 'nombre_usuario'@'localhost';

Change the localhost to the IP address where you are going to connect or you could put % to connect from any IP address, but you should keep in mind that when using % you leave the base as it is they could connect from anywhere to it.

If you need more information to create users, I leave this link: link

    
answered by 27.12.2017 в 13:25