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

jinhwa yoo
jinhwa yoo
10,042 Points

help me plz

I don't get it how to understand and code this case..

I know how to query to the database but.. next????

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


  $results->query("SELECT member_id, email, fullname, level FROM members");
Simon Coates
Simon Coates
28,694 Points

the query code already ran in helper.php.

1 Answer

Simon Coates
Simon Coates
28,694 Points

The instructions state that "The data from the database is currently in a PDOStatement object named $results". So the solution is:

<?php
include "helper.php";

/* 
 * helper.php contains
 * $results->query("SELECT member_id, email, fullname, level FROM members");
 */
$recordSet = $results->fetchAll();
foreach($recordSet as $record){
   send_offer($record['member_id'], $record['email'], $record['fullname'], $record['level']);
}
jinhwa yoo
jinhwa yoo
10,042 Points

$recordSet = $results->fetchAll(); foreach($recordSet as $record){ send_offer($record['member_id'], $record['email'], $record['fullname'], $record['level']); } ----->>why put $recordSet and why you use foreach???

Simon Coates
Simon Coates
28,694 Points

$recordSet is just used to store an Array returned by calling fetchall. you don't actually need to create a variable. You could use:

foreach($results->fetchAll() as $record){
   send_offer($record['member_id'], $record['email'], $record['fullname'], $record['level']);
}

Foreach is used to operate on each element in the array you get by calling fetchall on the results object.