Problems with database and array

0

I'm trying to do something as simple as a connection to a database and treat the data with an array, but there's no way I'm blocked. I'm getting an error I'm looking for, but there's no way.

// control

 <?php 

    include('../include/conexion.php');

    $user = $_POST['usuario'];
    $pass = $_POST['contrasena'];

    //var_dump($registro);

    foreach ($registro as $registros) {
        $pass_bd = $fila['contrasena'];
        if (password_verify($pass, $pass_bd)) {
        echo "Las contraseñas son iguales";

        }
        else{
            echo "error";
        }
    }
?>

// connection

<?php

    $host = 'localhost';
    $user = 'test';
    $pass = '1234';
    $db = 'mc_17';

    $conexion = mysqli_connect($host,$user,$pass,$db);

    mysqli_query($conexion, 'SET NAMES "utf-8"');

    $sentencia = 'SELECT * FROM Usuarios';
    $result = mysqli_query($conexion, $sentencia);

    while ($fila = mysqli_fetch_assoc($result)) {
        $registro[] = $fila;
    }

    //La siguiente me da un error el cual no consigo solucionar ni encontrar info exacta sobre este error.
    mysqli_free_result($result);
    mysqli_close($conexion);
?>
    
asked by sergibarca 24.01.2018 в 10:51
source

1 answer

1

In conexion you are creating the array in the correct way. It is not necessary to use array_push to do so.

Your error is reading the array that returns conexion , specifically here:

foreach ($registro as $registros) {

It should be:

foreach ($registro as $fila) {

since then, within the loop, you try to access the elements like this:

$fila['contrasena']

If you put it that way it should work:

<?php 

    include('../include/conexion.php');

    $user = $_POST['usuario'];
    $pass = $_POST['contrasena'];

    //var_dump($registro);

    foreach ($registro as $fila) {
        $pass_bd = $fila['contrasena'];
        if (password_verify($pass, $pass_bd)) {
            echo "Las contraseñas son iguales";
        }
        else{
            echo "error";
        }
    }
?>
    
answered by 24.01.2018 в 17:05