PHP Sample Method for Using PDO Abstraction Layer to Get Query Results

  • 2021-09-24 21:55:20
  • OfStack

This article illustrates how PHP uses PDO abstraction layer to obtain query results. Share it for your reference, as follows:

PHP uses PDO abstraction layer to obtain query results in three main ways:

(1) PDO:: query () query.

Look at the following php code:


<?php
//PDO::query() Query 
$psql="SELECT * FROM user";
$res = $db->query($psql);
$res->setFetchMode(PDO::FETCH_NUM); // Digital indexing mode 
while ($row = $res->fetch()){
print_r($row);
}
?>

(2) PDO- > exec () Processes sql


<?php
//PDO->exec() Deal with sql
$psql="INSERT INTO user(id , username) values('' , ' Zhang 3')";
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$res = $db->exec($psql);
echo $res;
?>

(3) PDO:: prepare () preprocessing executes queries


<?php
//PDO::prepare() Preprocessing executes queries 
$psql="SELECT * FROM user";
$res = $db->prepare($psql);
$res->execute();
while ($row = $res->fetchAll()) {
print_r($row);
}
?>

setAttribute() The method is to set the properties, and the common parameters are as follows:

PDO:: CASE_LOWER--Force column names to be lowercase
PDO:: CASE_NATURAL--Column name as original
PDO:: CASE_UPPER--Force column name to uppercase

setFetchMode Method to set the type of return value to get the result set. The common parameters are as follows:

PDO:: FETCH_ASSOC--associative array form
PDO:: FETCH_NUM--Number index array form
PDO:: FETCH_BOTH--Both in array form, which is the default
PDO:: FETCH_OBJ--In the form of an object, similar to the previous mysql_fetch_object ()

The above is summarized as follows:

Query operations are mainly PDO::query() , PDO::exec() , PDO::prepare() .

PDO->query() --Processes 1 SQL statement and returns 1 "PDOStatement"
PDO->exec() --Processes 1 SQL statement and returns the number of entries affected

PDO::prepare() It is mainly a preprocessing operation, which needs to be passed through $rs- > execute () to execute the SQL statement in the preprocessing

Finally, two commonly used functions are introduced:

(1) fetchColumn() Get the result of 1 field in the specified record, and the default is the first field!


<?php
$psql="SELECT * FROM user";
$res = $db->query($psql);
// Gets the number in the specified record 2 Field results 
$col = $res->fetchColumn(1);
echo $col;
?>

(2) fetchAll() Get data from 1 result set and store it in associative array


<?php
$psql="SELECT * FROM user";
$res = $db->query($psql);
$res_arr =$res->fetchAll();
print_r($res_arr);
?>

For more readers interested in PHP related content, please check the topics on this site: "Summary of PHP database operation skills based on pdo", "Summary of php+Oracle database programming skills", "Encyclopedia of PHP+MongoDB database operation skills", "Introduction to php object-oriented programming", "Usage summary of php string (string)", "Introduction to php+mysql database operation skills" and "Summary of php common database operation skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: