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

Martin Batista
Martin Batista
4,606 Points

How would you go about looping the PDOStatement object named $results and then pass the data to the send_offer function?

I'm baffled at the moment. Need to be pointed in right direction.

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

/* 
 * helper.php contains
 * $results->query("SELECT member_id, email, fullname, level FROM members");
 */
$mem_id;
$email;
$fullname;
$level;

foreach($results as result){
    $result->bindParam(':member_id', $mem_id, PDO::PARAM_INT);
    $result->bindParam(':email', $email, PDO::PARAM_STR, 20);
    $result->bindParam(':fullname', $fullname, PDO::PARAM_STR, 20);
    $result->bindParam(':level', $level, PDO::PARAM_STR, 20);
    $result->execute();
}
  function send_offer($mem_id, $eml, $Fullnme, $lev) {


}

4 Answers

Simon Coates
Simon Coates
28,694 Points

<?php include "helper.php";

$rows = $results->fetchall();

foreach($rows as $row){ send_offer($row['member_id'], $row['email'], $row['fullname'], $row['level'] ); }

you seem to have gotten real confused. The above code will pass the challenge i think. You retrieve the results, iterate over them, while calling the send offer method (don't need to define this.). I don't typically post precise code, but it might help isolate where you've gone off.

Martin Batista
Martin Batista
4,606 Points

Hi Simon, I believe your code is perfect.

Make's total logical sense. Thank you!

Martin

Paul Yabsley
Paul Yabsley
46,713 Points

Hi Martin

Its difficult to say exactly what you need without seeing all of what is in helper.php.

<?php
// $results->query("SELECT member_id, email, fullname, level FROM members");

// This is your query but I assume before this you have a pdo object set up
// and you've created an instance of it with $results

// In order to get the results of your query as an associative array from the $results object
// you have to call one of the fetch pdo methods. Either fetch (http://php.net/manual/en/pdostatement.fetch.php)
// or fetchAll (http://php.net/manual/en/pdostatement.fetchall.php)

// As your query doesn't have a WHERE clause I assume you are getting multiple rows so use fetchAll. 
// You do that like this:

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

// $actual_results now contains the results of your query as an associative array that you can loop through

foreach ($actual_results as $result) {
    // Now you can do what you want with each row for example:
    echo $result["fullname"];
}

// If you wanted to pass the array of results to your send_offer function you could do that like this:

$foo = send_offer($actual_results);

// You would need to change your function to accept an array instead of individual values though
// I don't understand what it is doing at the moment, I don't think you intend to bindParams inside it?
// You would usually bindParams as part of a PDO stored procedure bit like this:

try {
    $sth = $db->prepare("SELECT * FROM table WHERE col = :param");
    $sth->bindParam(':param', $param, PDO::PARAM_STR);
    $sth->execute();
} catch (PDOException $e) {
    // Error handling here
}
$results = $sth->fetchAll(PDO::FETCH_ASSOC);
Martin Batista
Martin Batista
4,606 Points

Thank you Paul!

the following code posted by Simon Coates, worked perfectly for the code challenge:

$rows = $results->fetchall();

foreach($rows as $row){ send_offer($row['member_id'], $row['email'], $row['fullname'], $row['level'] ); }

foreach($results as $member){ 
  send_offer(
    $member['member_id'],
    $member['email'],
    $member['fullname'],
    $member['level'] 
  ); }

You need a foreach loop.

You can simplify your code by omitting $rows and just using foreach ($results as $row) {

This task basically just asks you to do two things you've done before (foreach loops, and functions) but it really just wants you to use the values from results.