Detailed description of fetchAll () method for getting result sets in PDO

Source: Internet
Author: User
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?>
         
 
FetchAll (PDO: FETCH_ASSOC); // Obtain all data in the result set. For ($ I = 0; $ I GetMessage ().'') ;}?>
Id User name Password

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.