Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP & Databases with PDO PDO Database Security Cleanup & Final Steps

$film returns array for me, not boolean. Code looks identical except I use exit(), not die(). Any ideas?

My var_dump($film) returns an array with the individual movie's full data, not a boolean like in the video. I cannot figure out why. Here is the code:

<?php

require_once('database.php');

if (!empty($_GET['id'])) {
  $film_id = intval($_GET['id']);

  try {
    $results = $db->prepare('select * from film where film_id = ?');
    $results->bindParam(1, $film_id);
    $results->execute();
  } catch(Exception $e) {
      echo $e->getMessage();
      exit();
  }

  $film = $results->fetch(PDO::FETCH_ASSOC);
var_dump($film);
}



?>

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">
  <title>PHP Data Objects</title>
  <link rel="stylesheet" href="style.css">

</head>

<body id="home">

  <h1>Sakila Sample Database</h1>

  <h2>
  <?php 
    if (isset($film)) {
    echo $film['title'];
    } else {
      echo 'Sorry, no film was found with the provided ID.';
    }
  ?>
  </h2>
</body>

</html>

And the database.php:

<?php

// Remove error code prior to production.
ini_set('display_errors', 'On');

try{
  $db = new PDO('sqlite:./database.db');
  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(Exception $e) {
  echo $e->getMessage();
  die();
}


?>

1 Answer

I figured it out. I was entering a valid id, which returned the data properly. When I entered an invalid value I got the false as in the video.