0

The following code return some results that are baffling me...

if(is_array($loginUser)) {         
    $_SESSION['name'] = $loginUser['firstname'];
    $_SESSION['id'] = $loginUser['id'];
    print_r($_SESSION['name']);
    print_r($loginUser);
    var_dump($loginUser[1]);
    exit();
    header("Location: ../index.php?page=home");
}

That returns:

Notice: Undefined index: firstname in wwwroot/includes/userhandler.php on line 124

Notice: Undefined index: id in wwwroot/includes/userhandler.php on line 125

Array ( [0] => Array ( [0] => 4 [id] => 4 [1] => Johnny [firstname] => Johnny [2] => Appleseed [lastname] => Appleseed [3] => [email protected] [email] => [email protected] [4] => johnny'shashedpassword [password] => johnny'shashedpassword ) ) 

Notice: Undefined offset: 1 in wwwroot/includes/userhandler.php on line 129

NULL

4
  • 1
    Try with $_SESSION['name'] =$loginUser[0]['firstname']; Commented Sep 15, 2015 at 9:38
  • And that seems to work. Commented Sep 15, 2015 at 9:48
  • Do a $loginUser = current($loginUser); to fix your problem Commented Sep 15, 2015 at 10:24
  • @MohammadAlabed Because something is obvious to you, that doesn't mean it is obvious to others as well. Commented Sep 15, 2015 at 10:30

2 Answers 2

1

The id and firstName are stored in an array with key 0

change

$_SESSION['name'] = $loginUser['firstname'];
$_SESSION['id'] = $loginUser['id'];

to

$_SESSION['name'] = $loginUser[0]['firstname'];
$_SESSION['id'] = $loginUser[0]['id'];

but I see no reason why the you have an extra dimension in your $loginUser array. Try to refactor the way $loginUser is building its array

Later in the code you are calling var_dump($loginUser[1]); This returns your second error message because there is only a $loginUser[0]

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the explanation. I'm looking at it now to figure out why there are two arrays. I have never run into this before, and it certainly was not intentional.
no problem, we where all beginners once don't let people like Mohammad Alabed put you down. Keep programming and you will be a professional one day
0

Your array is in another array. You can solve this by using the following line of code:

$loginUser = current($loginUser);

Furthermore, when using var_dump I always find it easy to use <pre /> tags. This makes it easier for you to see how the array is built up.

Example:

echo "<pre>";
var_dump($loginUser);
echo "</pre>";
exit;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.