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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Task 1: Not retrieving the correct details from the database.

Howdy!

Okay, so I think I'm close to answering the first challenge but the "bummer" message tells me I'm not retrieving the correct details from the database.

Complete the function to return an ASSOCIATIVE ARRAY of 'people' from a database. Order these results in descending order by 'last_name'.
   + The function get_people has been started for you, and contains a connection to the database.
   + The connection object is named $db.
   + In the database is a table named 'people'.

So according to the question there is a last_name field but since there's no other detaila of what's in the people table, I thought I'd use the * operator and I am sure ORDER BY is the last statement in the query. HELP! :smile:.

index.php
<?php
function get_people() {
    include 'connection.php';

    //add code here

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

  return array();
}
minh nguyen
minh nguyen
55,848 Points

For task 1:

<?php function get_people() { include 'connection.php';

//add code here
$result = $db->prepare('SELECT * FROM people ORDER BY last_name DESC');
$result->execute();
return $result->fetchAll(PDO::FETCH_ASSOC);

}

For task2:

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

//add code here
if($filter){
  $where = ' WHERE treehouse=1';
}
$result = $db->prepare('SELECT * FROM people'. $where .' ORDER BY last_name DESC');
$result->execute();
return $result->fetchAll(PDO::FETCH_ASSOC);

}

2 Answers

geoffrey
geoffrey
28,736 Points

Hi Jonathan,

Here is what I have just done to pass the test.

<?php

function get_people() {

  include 'connection.php';

  $sql ='SELECT * FROM `people` ORDER BY last_name DESC';

  try {

    $results = $db->query($sql);
    $results->execute();

  } catch(Exception $e) {

     echo "Error: Unable to retrieve query. " . $e->getMessage();
     return array();

  }
  return $results->fetchAll(PDO::FETCH_ASSOC);
}
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Thanks George,

I've long since completed the course now of course but I'll mark this as a best answer for anyone else who happens to come long the thread! :)

geoffrey
geoffrey
28,736 Points

Jonathan Grieve Oops sorry, I had not noticed the topic was quite old when I answered some hours ago. You can call me Geoffrey ;)