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 trialGeoffrey Neal
30,298 Pointsintval() not working.
In the video Hampton uses the intval() method to force the $film_id variable into an integer. He then demonstrates it working by typing letters into the url in between the numbers of the $film_id (00:56). When he does it, it returns the film title corresponding to the first numbers in $film_id, but when I do it, it returns the same error message it did before using intval(): SQLSTATE[HY000]: General error: 1 unrecognized token: "e.g: 7hgch76". The intval() method appears to have done nothing, any guesses as to why?
Here is my code, I'm pretty sure it's exactly the same but maybe I've missed something:
<?php
require_once('database.php');
if(!empty($_GET["id"])) {
$film_id = intval($_GET["id"]);
}
try{
$results = $db->query('SELECT * FROM film WHERE film_id = ' . $_GET["id"]);
} catch(Exception $e) {
echo $e->getMessage();
die();
}
$film = $results->fetch(PDO::FETCH_ASSOC);
?>
<!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 echo $film["title"]; ?></h2>
</body>
</html>
Thanks in advance.
1 Answer
Matthew Bilz
15,829 PointsHi there -
I'm guessing it's because in you're try block, you still have the URL returning the $_GET "ID" and not the intval variable $film_id, perhaps try this:
try{
$results = $db->query('SELECT * FROM film WHERE film_id = ' . $film_id);
Geoffrey Neal
30,298 PointsGeoffrey Neal
30,298 PointsWorks perfectly, thanks Matthew.