Can not use object of type stdClass as array

1

I made a code to verify that the user and email are already exist, but when the user exists it returns the following error:

  

Fatal error: Fatal error: Can not use object of type stdClass as array

$query_check_user_name = $this->db->prepare('SELECT username, mail FROM users WHERE username= :user_name OR
mail = :user_email');
$query_check_user_name->bindValue(':user_name', $user_name, PDO::PARAM_STR);
$query_check_user_name->bindValue(':user_email', $user_email, PDO::PARAM_STR);
$query_check_user_name->execute();
$result = $query_check_user_name->fetchAll();

if (count($result) > 0) {
    for ($i = 0; $i < count($result); $i++) {
        $this->errors[] = ($result[$i]['user_name'] == $user_name) ? MESSAGE_USERNAME_EXISTS : MESSAGE_EMAIL_ALREADY_EXISTS;
    }
} else {
}
    
asked by anonymous 14.07.2015 / 17:48

1 answer

2

Try this

 $query_check_user_name->fetchAll(PDO::FETCH_ASSOC);

PHP usually returns this error when you have any object (as long as you do not implement the ArrayAccess interface) and try to access it as array .

Example

$objeto = new stdClass;

$objeto->nome = 'Wallace';

$objeto['nome']; // Fatal Error:  Cannot use object of type stdClass as array
    
14.07.2015 / 18:03