undefined index array pdo

-1

I'm trying to get the id of a user that logged in and saved it in the session, but it's not working, I make a query in the database at the time of logging and I transform the result into an array to get the id and store it in the session , but this is giving undefined index id, ta ai the code that logs in

     try{
 $select = ("SELECT * FROM usuario WHERE nome=:usuario AND senha=:senha AND status=1");
 $logarusuario = $pdo->prepare($select);
 $logarusuario->bindValue(":usuario", $usuario);
 $logarusuario->bindValue(":senha", $senha);
 $logarusuario->execute();
 $linhas = $logarusuario->rowCount();
 $result = $logarusuario->fetchAll(PDO::FETCH_OBJ);


 if($linhas > 0){
    $_SESSION['usuario'] = $usuario;
    $_SESSION['senha'] = $senha;
    $_SESSION['id'] = $result['id'];
    $_SESSION['logged_success'] = 'você foi logado com sucesso';
    header(5,"Location:../logado.php");
   }else{
    header("Location:../index.php");
    echo "Dados Digitados Incorretos";
   }
}catch(PDOException $e){
   echo $e;
}
?>
    
asked by anonymous 14.07.2016 / 01:21

1 answer

0

The fetchAll function returns a multidimensional array. You should not only do $result['id'] . Example of what is returned:

Array
(
    [0] => Array
        (
            [id] => 1

        ),
    [1] => Array
        (
            [id] => 2

        )
)

Then, you could do $result[0]['id'] , assuming there will always be a user returned in that snippet of code.

    
14.07.2016 / 22:22