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 trialTinashe Zikatih
5,598 Pointsguys please please help im totally stuck my head is spinning
im now confused dont even know what to do ive tried all i can
<?php
function get_people() {
include 'connection.php';
//add code here
if ($filter){
$sql = 'SELECT * FROM people WHERE treehouse = 1 ORDER BY last_name DESC';
}
try {
$results = $db->query("SELECT * FROM people ORDER BY last_name DESC");
} catch(Exception $e) {
echo "Error: Unable to retrieve query. " . $e->getMessage();
}
return $results->fetchAll(PDO::FETCH_ASSOC);
}
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! I think you're doing pretty well here. You obviously cleared task 1 without a problem. But there are a few things going on with task 2 and I know it can probably feel a bit overwhelming. First, the function is supposed to take an optional parameter. We make a parameter optional by simply giving it a default value in the definition of the function.
Secondly, your SQL statement is just fine. However, it says what the sql statement should be if the $filter is set. But you never say what it should be if the filter is not set.
Third, you then hard-code the SQL statement to be run instead of using the $sql
variable that you built which should take into account if the $filter was set or not. So at this point, no matter what, it's going to show the results as if it were unfiltered.
We want to prepare our $results by using a prepare method and sending in the appropriate $sql string.
Take a look:
<?php
function get_people($filter = null) { // optional parameter set to not filter by default
include 'connection.php';
//add code here
$sql = 'SELECT * FROM people ORDER BY last_name DESC'; // the default SQL statement to be run
if($filter){
$sql = 'SELECT * FROM people WHERE treehouse = 1 ORDER BY last_name DESC'; //The SQL statement to be run if we want it filters
}
try {
$results = $db->prepare($sql); //prepare our results variable using the filtered/unfiltered SQL statement as appropriate
$results->execute(); // execute the preparation
// everything below, you did just fine!
} catch(Exception $e) {
echo "Error: Unable to retrieve query. " . $e->getMessage();
}
return $results->fetchAll(PDO::FETCH_ASSOC);
}
I hope this helps, but let me know if you need more clarification!
Tinashe Zikatih
5,598 Pointsooh i realised where i had made a mistake thanks a milli...
Tinashe Zikatih
5,598 PointsTinashe Zikatih
5,598 Pointsummm i tried it its not working
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherTinashe Zikatih I'm not sure what to tell you. If I restart the challenge and copy/paste in the code that is in the black box above, it passes both steps.