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 trialDelroy Lucan Muyambo
5,445 PointsCRUD operations with php
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'.
Need help guys. im getting "It doesn't look like you have returned the correct information from the database."
<?php function get_people() { include 'connection.php';
//add code here
try{ return $db->query('SELECT * FROM people ORDER BY last_name, DESC');
} catch (PDOException $e) { echo "Error" . $e->getMessage() . "</br>"; return array(); } }
sagnikchakraborti
929 PointsWhich framework are you using for your php code?
Delroy Lucan Muyambo
5,445 Pointsim getting, "Bummer! It doesn't look like you have returned the correct information from the database" if i run this:
<?php function get_people() { include 'connection.php';
//add code here
try{ return $db->query('SELECT * FROM people ORDER BY last_name, DESC');
} catch (PDOException $e) { echo "Error" . $e->getMessage() . "</br>"; return array(); } }
1 Answer
Jonathan Grieve
Treehouse Moderator 91,253 PointsHi Delroy
It looks like you're struggling with the wrong return value. I had a bit of trouble with this challenge too :)
<?php
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);
}
?>
You want to return an associative array
but you can only use a return statement once, (or once within an one if or else block) and typically at the end of a function. You also want to return all the records from the query, so for that you need to use a different method.
So... you got your query right and it's good that you put the query in a try block. :)
You should also put the query in a variable e.g. $results
And then return the variable with the fetchAll() method making sure you tell PHP it's associative array! :-)
Hope this helps :)
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsHi Delroy, what is it you need help with in particular? :)