Accessing Databases: How to Fetch a Result Set


show more Fetching a result set provides you with in-depth training on Developer. Taught by David Powers as part of the Accessing Databases with Object-Oriented PHP show less
please wait ...

Fetching a result set

PDO has four different methods for fetching results from a database query. The one you're most likely to use is fetch, which gets the next row from a results set. FetchAll gets all the results at once and stores them as a multi-dimensional array. FetchColumn gets a single column from the next row. You specify which column you want by passing the method, the column number counting from zero as an argument. If no argument is passed, the first column is returned.

FetchObject returns the next row as an object. We'll look at creating objects from database results in chapter four. In the meantime let's concentrate on the first three methods. This is pdo_fetch.php which you can find in the chapter 2 02_04 folder of the exercise files. The try block at the top of the page includes the database connection on line three, then on line four we've got a SELECT query, which is stored in a variable called $sql.

We need to execute the query and store the result set. So we can do that on the next line. We...

Fetching a result set
Video duration: 8m 3s 3h 47m Intermediate

Viewers:

Fetching a result set provides you with in-depth training on Developer. Taught by David Powers as part of the Accessing Databases with Object-Oriented PHP

Subject:
Developer
Software:
PHP
Author:
please wait ...