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 Integrating PHP with Databases Querying the Database with PHP Working with Results

Kevin Costello
Kevin Costello
2,906 Points

Unable to use the send_offer function properly

I have tried to use the data from the $results variable in the send_offer function but I am unable to get it to work properly. I keep getting the following error message:

"Bummer! Doesn't look like you are passing the correct values."

Can anyone see what is wrong with my code?

Thanks,

Kevin

index.php
<?php
include "helper.php";

try {
    $results = $db->query(
        "SELECT member_id, email, fullname, level FROM members"
    );
} catch (Exception $e) {
    echo $e->getMessage();
}
//add code below this line

$loop = $results->fetchAll(PDO::FETCH_ASSOC);

$member_id = $loop->member_id;
$email = $loop->email;
$fullname = $loop->fullname;
$level = $loop->level;

send_offer($member_id, $email, $fullname, $level);

1 Answer

This looks good so far! The only thing missing is looping through the results of $results->fetchAll. fetchAll returns a collection of PDO objects, which can be treated just like an array.

So for each member in the result set, you need to set $member_id, $email, $fullname, $level, and call send_offer:

$members = $results->fetchAll(PDO::FETCH_ASSOC);
foreach ($members as $member) {
 . . . 
}
Kevin Costello
Kevin Costello
2,906 Points

Ok, that make sense, I hadn't thought that it would need to be looped through a foreach loop. I have added this into my code now and it was successful, thanks very much for your help!