Using Fetch Choices

Started by chinmay.sahoo, 06-30-2016, 05:59:27

Previous topic - Next topic

chinmay.sahooTopic starter

When you use prepare and execute, you have the choice of a number of formats in which data can be returned. The example we saw used the PDO::FETCH_ASSOC option with the fetch method, because it returns data in a format that will be very familiar for PHP4 users: an associative array.

If you'd rather use only object-oriented code in your application, you could instead employ the fetchObject method, which, as the name implies, returns the result set as an object. Here's how the whileloop will look when the fetchObjectmethod is used:

Quotewhile ($row =
$stmt->fetchObject()
)
{
print $row->Name . "\t";
print $row->CountryCode . "\t";
print $row->Population . "\n";
}