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 CRUD Operations with PHP Reading and Writing Reports Filtering Data

filtering data

Can anyone help me out on this qn i cant get it right tried to follow up other peoples solution on community but cant follow up its actually causing me to be more confused.

My challenge on this link : https://teamtreehouse.com/library/crud-operations-with-php/reading-and-writing-reports/filtering-data

my solution is as follows: <?php

function get_people($filter = null) { include 'connection.php';

//add code here

if($filter){ try { $results = $db->query("SELECT * FROM people ORDER BY last_name DESC WHERE treehouse = 1"); } catch(Exception $e) { echo "Error: Unable to retrieve query. " . $e->getMessage(); } }

 return $results->fetchAll(PDO::FETCH_ASSOC);

}

Please help.

index.php
<?php

function get_people($filter = null) {
    include 'connection.php';

    //add code here
  if($filter){
   try {
       $results = $db->query("SELECT * FROM people ORDER BY last_name DESC WHERE treehouse = 1");
    } catch(Exception $e) {
        echo "Error: Unable to retrieve query. " . $e->getMessage();
    }
  }


     return $results->fetchAll(PDO::FETCH_ASSOC);


}

2 Answers

Daniel Li
Daniel Li
15,244 Points

There's 2 problems in your code. First the following query is incorrect.

SELECT * FROM people ORDER BY last_name DESC WHERE treehouse = 1

should be

SELECT * FROM people WHERE treehouse = 1 ORDER BY last_name DESC

Secondly, there needs to be an else block to handle cases where $filtered is not null.

Thank you Daniel Li but the moment i use else statement to control the $filtered is not null its now saying "Oops! It looks like Task 1 is no longer passing."

So now i no longer know what you meant to say cause before i had tried the ...else statement and i wouldnt pass and i removed it and it was saying something different. Help please.