PHP Databases PDO connections-Collection of common programming errors

Hey guys im having a little trouble with the PDO in php as the error it is returning is an undefined index. The code for the function and query and return of result is this:

function getUserDetails($user) {
   $db = connect();
try {
    $stmt = $db->prepare('SELECT name,addr AS address,team
FROM TreasureHunt.Player LEFT OUTER JOIN TreasureHunt.MemberOf ON (name=player) 
LEFT OUTER JOIN TreasureHunt.PlayerStats USING (player)
WHERE name=:user');

    $stmt->bindValue(':user', $user, PDO::PARAM_STR);

    $stmt->execute();
    $results = $stmt->fetchAll();
    $stmt->closeCursor();

} catch (PDOException $e) { 
    print "Error : " . $e->getMessage(); 
    die();
}
return $results;  
}

However when running the code for the index page i get an error that says Notice: Undefined index: name

The Code for the index is this:

try {
$details = getUserDetails($_SESSION['player']);
echo '

Name

 ',$details['name'];
echo '

Address

',$details['address'];
echo '

Current team

',$details['team'];
echo '

Hunts played

 ',$details['nhunts'];
echo '

Badges

';
foreach($details['badges'] as $badge) {
    echo '',$badge['name'],'
'; } } catch (Exception $e) { echo 'Cannot get user details'; }

my question is why is it throwing a notice and how do i go around this problem?

  1. fetchAll returns all results (potentially multiple rows) in a multidimensional array:

    array(
        0 => array(/* first row */),
        1 => array(/* second row */),
        ...
    )
    

    That’s why the array doesn’t have a direct index 'name', it needs to be [0]['name'].
    Or you shouldn’t fetchAll, just fetch.

Originally posted 2013-11-10 00:11:43.