Detailed explanation of the fetch() method for obtaining result sets in PDO

黄舟
Release: 2023-03-07 17:20:02
Original
18309 people have browsed it

Detailed explanation of the fetch() method for obtaining result sets in PDO

When we introduced how PHP operates MYSQL, we also introduced several methods for obtaining result sets. For those who are not familiar with the records, you can go to our special topic PHP Operation MySQL to review. There are also several methods to obtain the result set in PDO. We will introduce these methods one by one!

In the previous article "Three ways to execute SQL statements in PDO", we introduced three ways for PDO to execute SQL statements, so in this article we will introduce PDO acquisition The fetch() method, one of several methods of the result set!

The fetch() method obtains the next row of data in the result set. The specific 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 ]]] )
Copy after login

Parameter fetch_style: controls the return method of the result set. The optional values ​​​​are as follows: :

Value says Ming
PDO::FETCH_ASSOCassociative array form .
PDO::FETCH_NUMNumber index array form.
PDO::FETCH_BOTHBoth are available in array form, which is the default.
PDO::FETCH_OBJIn the form of an object, it is similar to the previous mysql_fetch_object() function.
PDO::FETCH_BOUNDReturns 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_LAZYReturns the results in 3 forms: associative array, numeric index array and object.

Parameter cursor_orientation: A scrolling cursor of the PDOStatement object that can obtain a specified row.

Parameter cursor_offset: The offset of the cursor.

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

First create a php file and use PDO Connect to the MySQL database, then define the SELECT query statement, use the prepare() and execute() methods to perform the query operation, then return the next row of data in the result set through the fetch() method, and set the result set to be returned in the form of an associative array, and finally use the while The statement completes the loop output of the data. The specific code is as follows:

prepare($query);//准备查询语句
    $res->execute();            //执行查询语句,并返回结果集
    ?>
    fetch(PDO::FETCH_ASSOC)){         // 循环输出查询结果集,并且设置结果集为关联数据形式。
        ?>
        getMessage().'
'); } ?>
id 用户名 密码
Copy after login

The final output is as follows:

Detailed explanation of the fetch() method for obtaining result sets in PDO

About the fetch() method of obtaining the result set in PDO, we That’s it for the introduction. You can try it in your local area and contact us to consolidate what you have learned. In the next article, we will continue to introduce to you the method of obtaining the result set in PDO. For details, please read "PDO Detailed explanation of the fetchAll() method to obtain the result set》!

The above is the detailed content of Detailed explanation of the fetch() method for obtaining result sets in PDO. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!