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 trialJonathan Grieve
Treehouse Moderator 91,253 PointsTask 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! .
<?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();
}
2 Answers
geoffrey
28,736 PointsHi 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
Treehouse Moderator 91,253 PointsThanks 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
28,736 PointsJonathan Grieve Oops sorry, I had not noticed the topic was quite old when I answered some hours ago. You can call me Geoffrey ;)
minh nguyen
55,848 Pointsminh nguyen
55,848 PointsFor task 1:
<?php function get_people() { include 'connection.php';
}
For task2:
<?php function get_people($filter=null) { include 'connection.php';
}