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

Source: Internet
Author: User
We also introduced several records in the result set when we introduced MYSQL operations in PHP. unfamiliar friends can go to our topic to review MySQL operations in PHP, there are also several methods in PDO to obtain the result set. we will introduce these methods one by one! Detailed description of fetch () method for getting result sets in PDO

We also introduced several records in the result set when we introduced MYSQL operations in PHP. unfamiliar friends can go to our topic to review MySQL operations in PHP, there are also several methods in PDO to obtain the result set. we will introduce these methods one by one!

In the last article "three methods for executing SQL statements in PDO", we introduced three methods for executing SQL statements in PDO, in this article, we will introduce the fetch () method of one of several methods for obtaining the result set by PDO!

The fetch () method gets the next row of data in the result set. the syntax format of this function is as follows:

mixed PDOStatement::fetch ([ int $fetch_style [, int $cursor_orientation = PDO::FETCH_ORI_NEXT [, int $cursor_offset = 0 ]]] )

The fetch_style parameter controls the return method of 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 The object format is similar to the previous mysql_fetch_object () function.
PDO: FETCH_BOUND Return the result in the form of a Boolean value and assign 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.

Cursor_orientation: a scroll cursor of the PDOStatement object. you can obtain a specified row.

Cursor_offset: the offset of the cursor.

The following example uses the fetch () method to obtain the data of the next row in the result set, and then uses the while statement to output the data cyclically in the database. The steps are as follows:

First, create a php file, connect to the MySQL database through PDO, then define the SELECT query statement, apply the prepare () and execute () methods to execute the query operation, and then use fetch () in the method returned result set, no colleagues set the result set to return data in the form of an associated array. Finally, the while statement is used to output the data cyclically. the code is as follows:

 Prepare ($ query); // prepare the query statement $ res-> execute (); // execute the query statement and return the result set?>
Fetch (PDO: FETCH_ASSOC) {// cyclically outputs the query result set and sets the result set as the associated data form. ?> GetMessage ().'') ;}?>
Id User name Password

The output is as follows:

We will introduce the fetch () method for getting the result set in PDO. you can try it on your own to consolidate what you have learned, in the following article, we will continue to introduce how to obtain the result set in PDO. For more information, see the detailed description of the fetchAll () method for obtaining the result set in PDO.

The above is the detailed description of the fetch () 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.