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 - PHP Tutorial
From the course: PHP: Accessing Databases with PDO and MySQLi
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…
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
-
-
-
-
-
-
-
Connecting to a database with MySQLi5m 57s
-
Setting the character set1m 57s
-
Submitting a SELECT query and getting the number of results4m 4s
-
Fetching the result7m 35s
-
Rewinding the result for reuse3m 20s
-
Handling non-SELECT queries5m 27s
-
Getting error messages5m 47s
-
Sanitizing user input with real_escape_string()4m 7s
-
-
-
-