error when querying in mysql from php Warning: mysqli_fetch_array () expects parameter 1 to be mysqli_result, boolean given in

0

When trying to make a query in mysql from php to start a session I get the following error: Warning: mysqli_fetch_array () expects parameter 1 to be mysqli_result, boolean given in C: \ xampp \ htdocs \ procesologin.php on line 10 after the query I have an if mysqli_fetch_array () and then an else,  I always go to the else, do not enter the if, right there is the error. could you tell me how to fix it this is the html code:

<!doctype html>
<html>
    <head>   
        <meta charset="utf-8">
        <title>Documento sin título</title>
    </head>
    <body>
        <form role="form" action="procesologin.php" method="POST">
        <input class="form-control" placeholder="Usuario" name="usuario" type="text" autofocus>
        <input class="form-control" placeholder="Contraseña" name="contrasena" type="password" value="">
        <input type="submit" name="submit" class="btn btn-lg btn-primary btn-block" value="Ingresar">
    </body>
</html>

this is the connection to the database:

<?php
$server = "localhost";
$user = "root";
$pass = "";
$db = "prueba";
$tabla_db1 = "usuario";
$conexion = new mysqli($server,$user,$pass,$db);
?>

and this is the process:

<?php
session_start();
include("conexionprueba.php");
$user = $_POST['usuario'];
$password = $_POST['contrasena'];
$resultado = mysqli_query($conexion,"SELECT * FROM usuarios WHERE user = $user AND password = $password");
if ($consulta = mysqli_fetch_array($resultado)){
    echo "sesion exitosa";}
else{
    echo "sesion no exitosa";}
?>

Thanks

    
asked by Daniel Suarez 27.03.2018 в 08:29
source

1 answer

1

You could use it more easily if you use objects according to the php documentation it should be done like this.

<?php
$mysqli = new mysqli("localhost", "mi_usuario", "mi_contraseña", "world");

/* comprobar la conexión */
if (mysqli_connect_errno()) {
    printf("Falló la conexión: %s\n", mysqli_connect_error());
    exit();
}

$consulta = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

if ($resultado = $mysqli->query($consulta)) {

    /* obtener el array de objetos */
    while ($obj = $resultado->fetch_object()) {
        printf ("%s (%s)\n", $obj->Name, $obj->CountryCode);
    }

    /* liberar el conjunto de resultados */
    $resultado->close();
}

/* cerrar la conexión */
$mysqli->close();
?>
    
answered by 27.03.2018 в 08:46