Introduction PDO get result set, have to introduce the PDO is if the execution of SQL statements, generally divided into three kinds,
1.query () method
The query () method is typically used to return a result set after a query is executed. The syntax is this: Pdostatement pdo::query (String sql);
Parameter SQL is easy to understand, which is the SQL statement to execute.
2.prepare () method and execute () method
Prepare () is one of the methods of preprocessing a statement, usually to do the preparation of the query, and then execute () to execute the query.
The above is a brief introduction of these three cases.
Ii. How PDO gets the result set
PDO gets a result set of three methods, namely fetch (), Fetchall (), and Fetchcolumn ().
First, you first build a table in the database, named T_user.
CREATE TABLE ' T_user ' (
' userid ' int (one) is not NULL,
' username ' varchar DEFAULT NULL,
' Usersex ' varchar (6) DEFAULT NULL,
' Userage ' int (6) DEFAULT NULL,
PRIMARY KEY (' userid ')
) Engine=innodb DEFAULT Charset=utf8;
Then, insert the data, very simply, as follows:
INSERT into ' t_user ' VALUES (' 1 ', ' Li ', ' Boy ', ' 23 ');
INSERT into ' t_user ' VALUES (' 2 ', ' Nadia ', ' Gril ', ' 20 ');
INSERT into ' t_user ' VALUES (' 3 ', ' Wang ', ' Boy ', ' 55 ');
Here's a brief look at how each method is used.
1.fetch () method
The Fecth () method is to get the next row of data for the result set.
Table I: Fetch_style optional values that control how the result set is returned
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. |
The specific code for the Ftech () method is as follows:
<table border= ' 1 ' >
<?php
$dbms = ' mysql ';//select MySQL Database
$host = ' 127.0.0.1 ';//Database host name
$dbName = ' dbtext ';//Database used
$user = "";//database connection user name
$pwd = "";//Database connection password
$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 T_user";
$result = $pdo->prepare ($query);
$result->execute ();
while ($res = $result->fetch (PDO::FETCH_ASSOC)) {?>
<tr>
<td><?php echo $res [' userid '];? ></td>
<td><?php echo $res [' username '];? ></td>
<td><?php echo $res [' userage '];? ></td>
<td><?php echo $res [' usersex '];? ></td>
</tr>
<?php}
}catch (Exception $e) {
Die ("Error!!!". $e->getmessage (). " <br> ");
}
?>
</table>
Results such as 1:
Figure 1
2.fetchAll () method
It is clear from the word that the Fetchall () method is to get all the rows of the result set.
PHP PDO get result set