Get the Fetch () method of the result set in PDO

Source: Internet
Author: User
Tags dsn
get the Fetch () method of the result set in PDO

We introduced PHP before the operation of MySQL, but also introduced a few records to get the result set, not familiar with the small partners can go to our special PHP operation MySQL Review, then there are several methods in PDO to get the result set, we will introduce these several methods!

In the previous three ways to execute SQL statements in PDO, we introduced the three ways PDO executes SQL statements, and in this article we will introduce the fetch () method of one of the several ways PDO gets a result set!

The fetch () method gets the next row of data in the result set, which has the following specific syntax format:

Mixed Pdostatement::fetch ([int $fetch _style [, int $cursor _orientation = pdo::fetch_ori_next [, int $cursor _offset = 0] ]] )

Parameter Fetch_style: Controls how the result set is returned, with the following table of selectable values:

Value Description
Pdo::fetch_assoc Associative array form.
Pdo::fetch_num A numeric index array form.
Pdo::fetch_both Both arrays have the form, which is the default.
Pdo::fetch_obj In the form of an object, similar to the previous mysql_fetch_object () function.
Pdo::fetch_bound Returns the result as a Boolean value and assigns the obtained column value to the variable specified in the Bindparam () method.
Pdo::fetch_lazy Returns the result as an associative array, a numeric index array, and an object in 3 form.

Parameter cursor_orientation:pdostatement a scroll cursor for the object that can get the specified row.

Parameter cursor_offset: The offset of the cursor.

The following example obtains the data from the next row in the result set through the fetch () method, and then applies the while statement to complete the loop output of the data in the database, as follows:

First create a PHP file, connect to the MySQL database via PDO, then define the Select query statement, apply the prepare () and execute () methods to perform the query operation, and then fetch () method returns the next row of data in the result set no colleague sets the result set to be returned as an associative array, and finally completes the loop output of the data through the while statement, with the following code:

<?phpheader ("content-type:text/html;    Charset=utf-8 ");                                  Set the encoding format of the page $dbms = "MySQL";                                The type of database $dbname = "PHP_CN";                                   The database name used is $user = "root";                                    Database user name used $pwd = "root";                              The database password used $host = "localhost";    Host name used $DSN = "$dbms: host= $host;d bname= $dbName"; try{$pdo =new PDO ($DSN, $user, $pwd);//Initialize a PDO object to create a database connection object $pdo            $query = "SELECT * from User",//SQL statement to be executed $res = $pdo->prepare ($query);//Prepare query Statement $res->execute (); Executes the query statement and returns the result set?> <table border= "1" width= "> <tr> <td height=" "align=" ce            Nter "valign=" Middle ">id</td> <td height=" "align=" center "valign=" Middle "> User name </td> &LT;TD height= "align=" center valign= "Middle" > Password </td> </tr> <?php while ($res ult= $res->fetch (Pdo::fetch_assoC) {//loops out the query result set and sets the result set as the associated data form.?> <tr> <td height= "align=" Center "valign=" Middle "><?php echo $result [" id "];? ></td> <td height= "align=" center "valign=" Middle "><?php echo $result [" username "];? ></td> <td height= "align=" center "valign=" Middle "><?php echo $result [" password "];? ></td> </tr><?php}}catch (Exception $e) {die ("error!:". $e->getmessage (). ' <br> ');}? ></table>

The final output is as follows:

About the Fetch () method of getting result set in PDO we introduce here, you can try to contact your own local, to consolidate what you learned is just, the following article we continue to introduce the method of obtaining result set in PDO, please read the fetchall of obtaining result set in PDO. () method Detailed "!

Related Article

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: 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.