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 - PHP Tutorial
From the course: PHP: Accessing Databases with PDO and MySQLi
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Creating a database source name2m 3s
-
Connecting to a database with PDO7m 27s
-
Looping directly over a SELECT query3m 49s
-
Fetching a result set8m 3s
-
Finding the number of results from a SELECT query7m 14s
-
Checking if a SELECT query contains results3m 32s
-
Executing simple non-SELECT queries6m 2s
-
Getting error messages7m 17s
-
Using the quote() method to sanitize user input3m 30s
-
-
-
-
-
-
-