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 the result

Fetching the result - PHP Tutorial

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

Start my 1-month free trial

Fetching the result

MySQL improved has five different methods for fetching the results of a select query. The most commonly used is fetch_assoc which gets the next row as an associative array. If you prefer to work with an indexed array, use the fetch_row method. The third method fetch_array offers you a choice. You can have an indexed array, an associative array or both. By default it returns an array that uses both the column name and its number counting from zero as the array keys. Consequently each value is listed twice. The fetch_all() method returns the complete result set as a multidimensional array, using the column numbers as the array keys for each row. But you also have the option of using the column names, or both. The final method, fetch_object(), returns the row as an object. If you pass in the name of a class it'll create an instance of that class using the current rows values as its properties. We'll look at fetch_object in chapter seven but let's see the other four methods in action…

Contents