No MysqlInd does not work fetch_assoc or get_result ()

2

I do not have mysqlInd then it does not work fetch_assoc sample Fatal error: Call to undefined method mysqli_stmt :: fetch_assoc () ni get_result() Following this solution I'm trying to store all the columns in the queried row in a session $_SESSION['usuario'] and then query column by column so $_SESSION['usuario']['email'] as imitating fetch_assoc but they are stored as a single array they do not differentiate between the columns if they are all stored together

$email=(empty($_POST['email']))    ? NULL : $_POST['email'];
$pwd  =(empty($_POST['password'])) ? NULL : $_POST['password'];

if ($email && $pwd && $conn){
    $sql='SELECT * FROM Usuarios WHERE email=? AND password=?';
    $stmt = $conn->prepare($sql);
    if($stmt){
        $stmt->bind_param("ss",$email,$pwd);
        $stmt->execute();
        $stmt->store_result();
        $filas=$stmt->num_rows;
        if($filas>0){
            $arrDatos=$stmt->fetch_assoc();
            $_SESSION['usuario']=$arrDatos;
            header('Location: consola.php');
            $stmt->close();
            $conn->close();
        }
        else
        {
            echo "No se encontraron registros";
        }
    }
    else
    {
    echo "Error en la consulta: ".$stmt->error;
    }   
}
else
{
    echo "Falta alguno de los datos del POST o la conexión es nula";
}
function myGetResult( $Statement ) {
    $RESULT = array();
    $Statement->store_result();
    for ( $i = 0; $i < $Statement->num_rows; $i++ ) {
        $Metadata = $Statement->result_metadata();
        $PARAMS = array();
        while ( $Field = $Metadata->fetch_field() ) {
            $PARAMS[] = &$RESULT[ $i ][ $Field->name ];
        }
        call_user_func_array( array( $Statement, 'bind_result' ), $PARAMS );
        $Statement->fetch();
    }
    return $RESULT;
}

Any advice?

    
asked by ger 20.06.2018 в 14:14
source

1 answer

3

The code you are using is a substitute of fetch_assoc which should be used when:

  • you use prepared queries
  • and you want to save your results in an associative array
  • and you do not want to use the cumbersome bind_result in the case of having many columns or because you do not want to use it and period
  • and you do not have the driver mysqlnd
  • installed

Then, the function% co_of% 1 would do the job that myGetResult should do when get_result exists. What you have to do is simply, pass the mysqlnd to $stmt , which will return an associative array that you can use as you want or add it to the session variable.

Let's see:

$email=(empty($_POST['email']))    ? NULL : $_POST['email'];
$pwd  =(empty($_POST['password'])) ? NULL : $_POST['password'];

if ($email && $pwd && $conn){
    $sql='SELECT * FROM Usuarios WHERE email=? AND password=?';
    $stmt = $conn->prepare($sql);
    if($stmt){
        $stmt->bind_param("ss",$email,$pwd);
        $stmt->execute();
            $arrDatos=myGetResult ($stmt);
            $_SESSION['usuario']=$arrDatos;
            var_dump($_SESSION['usuario']);
            /*Estas dos líneas no pueden ir después de la redirección*/
            $stmt->close();
            $conn->close();

            /*Descomenta la línea que sigue luego de probar la variable de sesión*/
            //header('Location: consola.php');
        }
        else
        {
            echo "No se encontraron registros";
        }
    }
    else
    {
    echo "Error en la consulta: ".$stmt->error;
    }   
}
else
{
    echo "Falta alguno de los datos del POST o la conexión es nula";
}



function myGetResult( $Statement ) {
    $RESULT = array();
    $Statement->store_result();
    for ( $i = 0; $i < $Statement->num_rows; $i++ ) {
        $Metadata = $Statement->result_metadata();
        $PARAMS = array();
        while ( $Field = $Metadata->fetch_field() ) {
            $PARAMS[] = &$RESULT[ $i ][ $Field->name ];
        }
        call_user_func_array( array( $Statement, 'bind_result' ), $PARAMS );
        $Statement->fetch();
    }
    return $RESULT;
}

1 The function was given as a solution to the problem posed in the question: How to obtain an associative array using queries prepared with mysqli?

    
answered by 20.06.2018 / 15:40
source