From the course: PHP: Accessing Databases with PDO and MySQLi

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Fetching a result set

Fetching a result set - PHP Tutorial

From the course: PHP: Accessing Databases with PDO and MySQLi

Start my 1-month free trial

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…

Contents