Why does PDO add [0] => 1 to the fetched array?

I’m running a COUNT() on the database. Just trying to understand why PDO adds [0]=>1 to the return array.


$sql = "SELECT COUNT(`user_id`) AS `user_exists`
					FROM `users`
					WHERE `user_username` = :username";

			$stmt = $this->conn->prepare($sql);

			$stmt->execute(array('username'=>$this->username));

			$test = $stmt->fetch();

When I run print_r($test) I get:


Array
(
    [valid_user] => 1
    [0] => 1
)

Why is [0]=>1 there?

Thanks

By default, pdo returns duplicate data for all queries. It returns a keyed list (the valid_user) as well as an indexed list (the 0). Set the default fetch mode when you create your connection object.


$conn = new PDO(...);
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);