获取PDO - $ handler vs $ stmt?

I am VERY new to PHP / PDO, so please be gentle...

I am trying to enter code into my database and then fetch it into a webpage. I am able to do the first but am having difficulty displaying it. I am wondering if it's because i'm trying to combine $stmt and $handler together?

This is my code for entering the information into the database:

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO survey (storename, receipt, date_visit)
    VALUES (:storename, :receipt, :date_visit)");
    $stmt->bindParam(':storename', $storename);
    $stmt->bindParam(':receipt', $receipt);
    $stmt->bindParam(':date_visit', $date_visit);

    // insert a row
    $storename = $_POST['storename'];
    $receipt = $_POST['receipt'];
    $date_visit = $_POST['date_visit'];

}
catch(PDOException $e)
{
    echo "Error: " . $e->getMessage();
}
$conn = null;

It works perfectly.

This is my code for fetching information from my database.

<?php

try {
    $handler = new PDO('mysql:host=localhost;dbname=test', 'test', 'test');
    $handler->setAttribute(PDO::ATRR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo $e->getMessage();
    die();
  }

class SurveyEntry {
    public $id, $storename, $receipt, $date_visited,
        $entry;

    public function __construct() {
        $this->entry = "{$this->storename} posted: {$this->receipt}";
    }
}

$query = $handler->query('SELECT * FROM survey');
$query->setFetchMode(PDO::FETCH_CLASS, 'SurveyEntry');
while($r = $query->fetch()) {
    echo $r->entry, '<br>';
}

?>

I can confirm that it connects correctly, but I can't get it to display any information. I'm wondering if it's something to do with the difference in $stmt and $handler that i'm using? I've been following tutorials online and have quite possibly mixed 2 tutorials together to try and achieve what i'm looking for.

UPDATE:

I managed to get it to work by updating how I called from the database:

$host = "localhost";
$dbname = "test";

$user = "test";
$password = "test";

$handler = new PDO( "mysql:dbname=$dbname;host=$host" , $user , $password );

Figured it out - I had 'ATRR_ERRMODE' instead of 'ATTR_ERRMODE' (typo)

how are you?

You should try to fix it:

1- Two different connections:

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);


$handler = new PDO('mysql:host=localhost;dbname=test', 'test', 'test');