I've been studying PHP for 2 months now as my first scripting language. For most of my problems i can easily find an answer online, but there's something about PDO that i can't seem to understand.
In order to retrieve data from a database I instantiate a new object of the PDO class and call the PDO::query() method on it. This returns a PDOStatement object which carries the result set from the SQL query. Here's where the problem starts. I can't seem to understand how and where the data from the result set is stored.
In the PHP Manual i learned to display the returned rows by iterating over the PDOStatement object with a foreach loop. However, the PHP manual clearly states that if an object is converted to an array, the result is an array whose elements are the object's properties. The PDOStatement only has one property - $queryString - containing the issued query string. So... where are the query results stored? And why can I reach them through an array with a foreach loop, but not outside of it?
// Instantiate new PDO object to establish a new connection with MySQL database
$db = new PDO('mysql:dbhost=localhost;dbname=world', 'root', 'secret');
// Execute SQL query - Returns a PDOStatement object
$result = $db->query("SELECT Name, Continent, Population FROM Country");
// Result set can be accessed with a foreach loop iterating over the PDOStatement object
foreach ($result as $row) {
echo "$row[Name] - $row[Continent] - $row[Population] <br />";
}
// Outside the foreach loop, $result cannot be accessed this way.
// This produces 'Cannot use object of type PDOStatement as array'
echo $result[0]['Name'];
Fetch data from a result set by calling one of the following fetch methods: To return a single row from a result set as an array or object, call the PDOStatement::fetch method. To return all of the rows from the result set as an array of arrays or objects, call the PDOStatement::fetchAll method.
System-missing values are always converted to the Python data type None. By default, user-missing values are converted to the Python data type None.
PDOStatement::fetchAll() returns an array containing all of the remaining rows in the result set. The array represents each row as either an array of column values or an object with properties corresponding to each column name. An empty array is returned if there are zero results to fetch.
PDO is an acronym for PHP Data Objects. PDO is a lean, consistent way to access databases. This means developers can write portable code much easier. PDO is not an abstraction layer like PearDB. PDO is a more like a data access layer which uses a unified API (Application Programming Interface).
The PDOStatement class implements the Iterator interface, which lets its objects be iterated through.
Iterator extends Traversable {
/* Methods */
abstract public mixed current ( void )
abstract public scalar key ( void )
abstract public void next ( void )
abstract public void rewind ( void )
abstract public boolean valid ( void )
}
For an object that implements the Iterator interface,
foreach($result as $row) {
// Code
}
is equivalent to
for ($result->rewind(); $result->valid(); $result->next()) {
$row = $result->current();
// Code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With