Handling the result

Both of the previous queries return a StatementInterface, which is iterable. So, to access its data, we can do this:

foreach ($result as $record) {
$id = $record->id;
$team_id = $record->team_id;
$name = $record->name;
$data = $record->data;
}

Each item in the loop is a stdClass, and their property names are the actual names of the columns returned, while their values are the column values.

Alternatively, the StatementInterface also has some fetcher methods that can prepare the results for us in different ways. These mostly come from the parent \PDOStatement class, which is native PHP. The simplest is fetchAll():

$records = $result->fetchAll();  

This returns an array of stdClass objects, as we saw before, so it does all the looping to extract the records for us. If we want this array keyed by the value of a field in the record, we can perform the following:

$records = $result->fetchAllAssoc('id');

This will use the value in the id field to key the array.

If we're expecting single records, we can also use the fetch() method, which returns only one such object (the next one in the result set); fetchObject() does the same thing.