0

I am implementing a simple user login using PDO. But it is giving me a warning message :

Notice: Undefined index: id in login.php on line 39.

Code:

<?php
if($_POST)
{

    $query = $db->prepare('SELECT * FROM users 
                           WHERE email = :email and password = :pass');

    $query->execute(array(':email' => $_POST['email'], 
                  ':pass'  => $_POST['password'])); 

    $count  = $query->rowCount();

    if($count==1)
    {
        $result         = $query->fetchAll();

        $_SESSION['user']   = $result['id'];

        header("location:index.php");   
        exit;   
    }
    else
    {
        $_SESSION['message']='Invalid login details.';
        header("location:login.php");   
        exit;   
    }
}
?>

What am I missing here?

Hassan Sardar
  • 4,413
  • 17
  • 56
  • 92

2 Answers2

2

Assuming you store unique usernames. You don't need to use fetchAll() here. You can use fetch();

$result = $query->fetch(PDO::FETCH_ASSOC);
echo $result['id'];
Jay Bhatt
  • 5,601
  • 5
  • 40
  • 62
0

fetchAll returns an array: http://www.php.net/manual/en/pdostatement.fetchall.php

So you'll need $result[0]['id']

Luceos
  • 6,629
  • 1
  • 35
  • 65