Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a PDO result set stored

Tags:

php

pdo

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'];
like image 540
user1440560 Avatar asked Jun 06 '12 23:06

user1440560


People also ask

How fetch data from database in PHP and display PDO?

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.

What is the default data type for the fetchAll () result?

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.

What does PDO fetchAll return?

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.

What is PDO in database?

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).


1 Answers

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
}
like image 134
Cameron Martin Avatar answered Oct 06 '22 00:10

Cameron Martin