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 trialRobbie Thomas
31,093 PointsPHP PDO Stage 2 End
This is not a code challenge here. I'm done with the end of stage 2 in the PHP PDO section and on my way to finishing stage 3 and the whole PHP Development all together. However, at the end of stage 2, I'm pretty sure I did the code right, but in the end, it seems I'm wrong. Here's my code:
<?php
require_once('database.php');
if(!empty($_GET['id'])){
$film_id = $_GET['id'];
}
try {
$results = $db->query('select * from film where film_id = '.$flim_id);
} catch(Exception $e) {
echo $e->getMessage();
die();
}
$film = $results->fetch(PDO::FETCH_ASSOC);
?>
And I get this:
Notice: Undefined variable: flim_id in /home/treehouse/workspace/films.php on line 10 SQLSTATE[HY000]: General error: 1 near " ": syntax error
Any idea what I did wrong? Again, it's not a code challenge, but I like to get this correct when or if I venture into PHP Development in the future.
R.T.
1 Answer
Ted Sumner
Courses Plus Student 17,967 PointsThe first thing that I notice is that you do not create a new instance of your database classe. You need a $db = new
line prior to your try catch block. The second thing I would do is change your try phrase to this:
<?php
try {
$results = $db->query("select * from film where film_id =$flim_id");
} //catch block starts here as you did above
One thing that I learned that was really helpful in coding PHP relates to variables and single and double quotes. If you use double quotes, the variable is resolved and the value is placed in the string in place of the variable. If you use single quotes the variable is not resolved and you will display the variable name, not its value. Thus, it is far easier to to use double quotes and not have to worry about concatenation in a string like you use in your try block.
Your film id needs to be in quotes. This is how I handled it in a project I am currently working on. I need to add a try catch block around the function, but the code works as is:
<?
public function getAllTopics($table, $constraint) {
$query = $this->connection->prepare("SELECT * FROM $table WHERE $constraint");
$query->setFetchMode(PDO::FETCH_ASSOC);
$query->execute();
return $query;
reset($query);
}
And this is how I assign $constraint in the index.php file:
<?php
$constraint = "badge = 'wolf'";
I felt the best way to accomplish the quote was to include it in the variable, but I may change my mind and try it in the class. I am not sure yet. The end code will build $constraint from user input.
Ted Sumner
Courses Plus Student 17,967 PointsTed Sumner
Courses Plus Student 17,967 Pointsformatted code quote. Please review for future reference on how to quote code and have it formatted.