当前位置:网站首页>Differences of several query methods in PDO
Differences of several query methods in PDO
2022-06-13 03:14:00 【wks19891215】
(1)exec
php The description in the manual is : To perform a SQL sentence , And return the number of rows affected .
It can be seen from it that ,execute It can be applied to “ Additions and deletions ” Additions, deletions and modifications in . Because the query operation will return a result set , and exec Function can only return the affected function .
(2)query
Corresponding to “ Additions and deletions ” Medium “ check ”. Used to execute once sql sentence , Return to one PDOStatement Result set . This result can be considered as a multidimensional array , You can use it directly .
(3)prepare
For repeated statements , have access to prepare Make a pretreatment ( Optimize ). Because each query statement needs to be compiled before execution , adopt prepare after , It can save the cost of repeatedly compiling query statements . First prepare Again execute. This returns a PDOStatement Result set ,PDOStatement It's an object , You can use its member methods to handle the result set . Depending on the result set type , It can also be used directly as a multidimensional array .
If prepare Incoming sql If the statement is mutable , Can pass bindParam To bind parameters to , then execute.
PS:prepare+execute The combination of , Can be applied to “ Additions and deletions ” All operations .
Let's take a look at specific usage examples :
<?php
$dsn = "mysql:dbname=pdo;host=localhost";
$user = "root";
$password = "root";
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Use exec To add, delete, or modify
$sqlcmd = "insert into userinfo values (rand(),rand())";
$dbh->exec($sqlcmd);
// Use query Query operation
$sqlcmd = "select * from userinfo";
try {
$data = $dbh->query($sqlcmd);
foreach ($data as $row) {
print_r($row);
echo '<br>';
}
}
catch (PDOException $e) {
echo $e->getMessage();
}
//prepare First pretreatment , Then bind the parameters , then execute
$sqlcmd = 'select password from userinfo where username = :pname';
$res = $dbh->prepare($sqlcmd);
$puser_name = "aaa";
$res->bindParam(':pname', $puser_name, PDO::PARAM_STR, 200);
$res->execute();
foreach ($res as $row) {
print_r($row);
echo "<br>";
}
?>
This is just the most basic use case . Generally speaking , It will be used in specific projects PDO Wrapper class , Some frames such as zendframework It also comes with its own database processing class .
边栏推荐
- Hash table: least recently used cache
- Code d'initialisation de l'arbre binaire
- . Net compact Framework2.0 wince intelligent device development project experience sharing Net drag space advanced
- Data Governance Series 1: data governance framework [interpretation and analysis]
- C language function strcmp() (compare two strings)
- . New features in net 6.0 _ What's new in net 6.0
- Redis server configuration
- Introduction to Kestrel_ Introduction to kestrel web server
- C method parameter: params
- [JVM Series 2] runtime data area
猜你喜欢
[JVM Series 7] garbage collector
AAR packaging and confusion
Wechat applet switch style rewriting
brew工具-“fatal: Could not resolve HEAD to a revision”错误解决
Vs Code modify default terminal_ Modify the default terminal opened by vs Code
Six special GPU products for domestic aircraft passed the appraisal and review
SQL execution process in MySQL (3)
The extra money we made in those years
Mp4 playback
Radio design and implementation in IVI system
随机推荐
Ijkplayer source code --- decode
Use and arrangement of wechat applet coordinate position interface (I)
Binary tree initialization code
Ijkplayer source code ---packetqueue
Keil去掉烦人的ST-Link更新提示
. New features in net 6.0 _ What's new in net 6.0
二叉树初始化代码
Stack information, GC statistics
2022 qianle micro cloud technology learning task plan
JVM virtual machine stack (III)
Introduction to Sitemap_ Sitemap online generation and sorting
Data Governance Series 1: data governance framework [interpretation and analysis]
Hash table: least recently used cache
Supervisor -- Process Manager
Solution of Kitti data set unable to download
JS deconstruction assignment
Linked list: the first coincident node of two linked lists
English语法_频率副词
【同步功能】2.0.16-19 版本都有同步功能修复的更新,但未解决问题
JVM class loader (2)