The fetchAll () method is to obtain all rows in the result set and return a binary array containing all rows in the result set!
Detailed description of fetchAll () method for getting result sets in PDO
The fetchAll () method is to obtain all rows in the result set and return a binary array containing all rows in the result set!
In the previous "fetch () method for obtaining result sets in PDO", we introduced the fetch () method for obtaining result sets. the fetchAll () method we will introduce today () the method is similar to the previous method fetch (), but this method can obtain all rows in the result set only once and assign them to the returned array (two-dimensional ).
The syntax format of the fetchAll () method is as follows:
array PDOStatement::fetchAll ([ int $fetch_style [, mixed $fetch_argument [, array $ctor_args = array() ]]] )
The fetch_style parameter controls the data return methods in the result set. the optional values are as follows:
Value |
Description |
PDO: FETCH_ASSOC |
Join array form |
PDO: FETCH_NUM |
Numeric index array format |
PDO: FETCH_BOTH |
Both arrays are available, which is the default |
PDO: FETCH_OBJ |
According to the object format, similar to the previous mysql_fetch_object () |
PDO: FETCH_BOUND |
Returns the result in the form of a Boolean value, and assigns the obtained column value to the variable specified in the bindParam () method. |
PDO: FETCH_LAZY |
Return results in three forms: correlated array, numeric index array, and object. |
Parameter column_index: field index!
The returned value is a two-dimensional array containing all the data in the result set.
In the following example, we use the fetchAll () method to obtain all rows in the result set, and read the data in the two-dimensional array through the for statement to complete the cyclic output of the data in the database. the specific steps are as follows:
Create a php file, connect to the MySQL database through PDO, define the SELECT query statement, apply the prepare () and execute () methods to execute the query operation, and then use fetchAll () the method returns all rows in the result set, and uses the for statement to output all the data in the result set cyclically. the code is as follows:
Prepare ($ query); // prepare the query statement $ res-> execute (); // execute the query statement and return the result set?>
Id |
User name |
Password |
FetchAll (PDO: FETCH_ASSOC); // Obtain all data in the result set. For ($ I = 0; $ I
|
|
|
GetMessage ().'') ;}?>
The output result is as follows:
We will first introduce the fetchAll () method for getting the result set in PDO. this method is similar to the fetch () method we introduced earlier. do not use an error, in the next article, we will continue to introduce the third method for obtaining the result set in PDO, the fetchColumn () method. for details, please read the detailed description of the fetchColumn () method for obtaining the result set in PDO!
The above is the detailed description of the fetchAll () method used to obtain the result set in PDO. For more information, see other related articles in the first PHP community!