Definition: PDO (php Data Object) is an extension supported by PHP5, which defines a lightweight, consistent interface for PHP access to a variety of databases.
PDO is a significant feature in PHP5, and PHP6 will only use PDO to process the database by default. And we'll use PDO to unify the operations of querying and retrieving data from various databases.
PHP pdo->query () querying data records and returning query resultspdo->query ()
Pdo->query () is used to query data records and return query results.
Grammar:
pdo->query () Pdo->query () is used to query data records and return query results.
Grammar:
Example:
<?php//construct PDO connection $DBH = "Mysql:host=localhost;dbname=test"; $db = new PDO ($dbh, ' root ', ' root123 '); $db->query ("Set character set ' GBK '"); Query Data $sql = "SELECT * from user"; $sth = $db->query ($sql); while ($row = $sth->fetch ()) { echo "user name:". $row [' username ']. " <br/> "; echo "Email:". $row [' email ']. " <br/> ";
In the example above, we modeled the MySQL Tutorial section of the query example, using the PDO approach to the User table query operation.
Pdostatement->fetch () The fetch () method in PDO is used to get a row of results from the result set, which behaves like mysql_fetch_array (), unlike the method, which not only returns an array but also returns an object.
Grammar:
Mode is an optional parameter that represents the type of result set you want to return, which defaults to the array form common to associative and numeric indexes.
The mode parameter has the following values:
Take value |
Description |
Pdo::fetch_assoc |
Associative index (field name) array form |
Pdo::fetch_num |
Numeric indexed Array Form |
Pdo::fetch_both |
By default, both associative and numeric indexed array forms have |
Pdo::fetch_obj |
In the form of an object |
Pdo::fetch_bound |
Assigning the value of a column to a variable by using the Bindcolumn () method |
Pdo::fetch_class |
Returns a result set in the form of a class that is automatically created if the specified class property does not exist |
Pdo::fetch_into |
Merge data into an existing class to return |
Pdo::fetch_lazy |
Combines Pdo::fetch_both and pdo::fetch_obj to create object variables when they are invoked |
Pdostatement->setfetchmode ()
If you do not specify a result type that is returned in fetch (), you can also use the Setfetchmode () method setting separately, such as:
... $sth = $db->query ($sql); $sth->setfetchmode (PDO::FETCH_ASSOC); while ($row = $result->fetch ()) {
Pdostatement->fetchall ()
The Fetchall () method is used to remove data from the dataset once and into the array.
Grammar:
Mode is an optional parameter that represents the array that you want to return, Column_index represents the column index number, which is specified when mode takes the value pdo::fetch_column.
The mode parameter has the following values:
Take value |
Description |
Pdo::fetch_column |
Specifies that a column in the returned result set is returned, with the specific column index specified by the Column_index parameter |
Pdo::fetch_unique |
Returns the result set as the first key value in the table below, followed by the following table |
Pdo::fetch_group |
Group by the value of the specified column |
Example:
$sth = $db->query ($sql); $row = $sth->fetchall (); Returns only username (index=1) $row = $sth->fetchall (pdo::fetch_column, 1);
Pdo->query (String statement)
Example:
<?php//construct PDO connection $DBH = \ "Mysql:host=localhost;dbname=test\"; $db = new PDO ($DBH, \ ' root\ ', \ ' root123\ '); $db->query (\ "set character set \ ' Gbk\ ' \"); Query data $sql = \ "SELECT * from User\"; $sth = $db->query ($sql); while ($row = $sth->fetch ()) { echo \ "user name: \". $row \[\ ' username\ ' \].\ "<br/>\"; echo \ "e-mail: \". $row \[\ ' email\ ' \].\ "<br/>\";
In the example above, we modeled the MySQL Tutorial section of the query example, using the PDO approach to the User table query operation.
Pdostatement->fetch ()
The fetch () method in PDO is used to get a row of results from the result set, which behaves like mysql_fetch_array (), unlike the method, which not only returns an array but also returns an object.
Grammar:
Mode is an optional parameter that represents the type of result set you want to return, which defaults to the array form common to associative and numeric indexes.
The mode parameter has the following values:
Take value |
Description |
Pdo::fetch_assoc |
Associative index (field name) array form |
Pdo::fetch_num |
Numeric indexed Array Form |
Pdo::fetch_both |
By default, both associative and numeric indexed array forms have |
Pdo::fetch_obj |
In the form of an object |
Pdo::fetch_bound |
Assigning the value of a column to a variable by using the Bindcolumn () method |
Pdo::fetch_class |
Returns a result set in the form of a class that is automatically created if the specified class property does not exist |
Pdo::fetch_into |
Merge data into an existing class to return |
Pdo::fetch_lazy |
Combines Pdo::fetch_both and pdo::fetch_obj to create object variables when they are invoked |
Pdostatement->setfetchmode ()
If you do not specify a result type that is returned in fetch (), you can also use the Setfetchmode () method setting separately, such as:
... $sth = $db->query ($sql); $sth->setfetchmode (PDO::FETCH_ASSOC); while ($row = $result->fetch ()) {
Pdostatement->fetchall ()
The Fetchall () method is used to remove data from the dataset once and into the array.
Grammar:
Mode is an optional parameter that represents the array that you want to return, Column_index represents the column index number, which is specified when mode takes the value pdo::fetch_column.
The mode parameter has the following values:
Take value |
Description |
Pdo::fetch_column |
Specifies that a column in the returned result set is returned, with the specific column index specified by the Column_index parameter |
Pdo::fetch_unique |
Returns the result set as the first key value in the table below, followed by the following table |
Pdo::fetch_group |
Group by the value of the specified column |
Example:
$sth = $db->query ($sql); $row = $sth->fetchall (); Returns only username (index=1) $row = $sth->fetchall (pdo::fetch_column, 1);