Fetchall () method of obtaining result set in PDO

Source: Internet
Author: User
Tags dsn
Fetchall () method of obtaining result set in PDO

The Fetchall () method is to get all the rows in the result set, returning a binary array containing all the rows in the result set!

So in the previous article "fetching the result set in PDO" in detail, we introduced the fetch () method to get the result set, and the Fetchall () method We are going to introduce today is similar to the previous method fetch (). However, the method only needs to be called once to get all the rows in the result set and assign to the returned array (two-dimensional).

The syntax format for the Fetchall () method is as follows:

Array Pdostatement::fetchall ([int $fetch _style [, Mixed $fetch _argument [, array $ctor _args = Array ()]])

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

Value Description
Pdo::fetch_assoc Associative array Form
Pdo::fetch_num Numeric indexed Array Form
Pdo::fetch_both Both array forms are available, which is the default
Pdo::fetch_obj In the form of an object, similar to the previous mysql_fetch_object ()
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 column_index: The index of the field!

Its return value is a two-dimensional array that contains all the data in the result set.

Here we get all the rows in the result set through the Fetchall () method, and through the For statement read the data in the two-dimensional array to complete the loop output of the data in the database, with the following steps:

First create the PHP file, connect the MySQL database via PDO, then define the Select query statement, apply the prepare () and execute () methods to perform the query operation, then return all rows in the result set through the Fetchall () method, and finally use the for Statement to complete the loop output of all the data in the result set, the code is as follows:

<?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 $result = $res ->fetchall (PDO::FETCH_ASSOC);       Gets all the data in the result set. for ($i =0; $i <count ($result); $i + +) {//loops through the data in a two-dimensional array.?> <tr> <td height= "a" al ign= "center" valign= "Middle" ><?php echo $result [$i] [' id '];? ></td> <td height= "align=" center "valign=" Middle "><?php echo $result [$i] [' username '];? ></td> <td height= "align=" center "valign=" Middle "><?php echo $result [$i] [' Password '];? ></td> </tr><?php}}catch (Exception $e) {die ("error!:". $e->getmessage (). ' <br> ');}? ></table>

The results of the output are as follows:

About the Fetchall () method for obtaining result sets in PDO let's introduce here, this method is similar to the fetch () method we introduced earlier, we must not use the wrong, the next article we will introduce the third method of obtaining result set in PDO, Fetchcolumn () method, please read the Fetchcolumn () method of obtaining result set in PDO.

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