Home > Backend Development > PHP Problem > What is PDO in PHP? What are the common ways to use pdo objects?

What is PDO in PHP? What are the common ways to use pdo objects?

慕斯
Release: 2023-03-11 06:00:01
Original
2097 people have browsed it

我们学习了解了这么多关于PHP的知识,不知道你们对PHP中PDO是什么?pdo对象的使用常见方法是什么?是否已经完全掌握了呢,如果没有,那就跟随本篇文章一起继续学习吧

相关推荐:php中如何调用存储函数?调用存储过程?其中触发器是什么?

PDO是什么?

PDO是别人写的“数据库操作工具类”!—一它可以代替我们自己写的MySQLDB.class.php.使用它,类似这样:

Spdo = new PDO(连接信息);ssql = "select " from .....";
Sresult = Spdo->query(Ssql);l/返回一个“pdo结果集”﹔$sql = "delete / update / insert.......""。
Sresult2 = Spdo->exec(Ssql);l/返回一个真假值﹔
Copy after login

手册可以找到:

函数参考》数据库扩展》数据库抽象层>PDO ,

具体应用的时候,其实,其中有这样的一个关系;

What is PDO in PHP? What are the common ways to use pdo objects?

即,要操作某种数据,就得去“打开”对应的pdo引擎。pdo引擎,在哪里打开?

——在phpini的配置文件中,无非就是一个“模块”而已,如下:

What is PDO in PHP? What are the common ways to use pdo objects?

当然,也得重启apache;

使用pdo连接mysql数据库:

$DSN = "mysql: host=服务器地址/名称: port=端口号; dbname=数据库名"
$0pt = array(PDd::MYsQL_ATTR_INIT_COMMAND=>’set names连接编码’);
$pdo = new pdo(SDSN,"用户名","密码",$opt);
Copy after login

pdo对象的使用(常见方法)

$result = Spdo->query("返回结果集的 sq1语句"");
Copy after login

结果:

成功:就是一个pdo结果集对象<后续马上学习〉,失败:false ,

Sresult = Spdo->exec("增删改的 sq1语句"");
Copy after login

结果: true<表示成功〉,false<表示失败>﹔

其他操作:

. $pdo->lastInsertId) ;
Copy after login

获取最后添加的id值

. spdo->beginTransaction() ;
Copy after login

开启一个事务

. $pdo->commit();
Copy after login

提交一个事务

. spdo->rollBack() ;
Copy after login

回滚一个事务

. $pdo->inTransaction();
Copy after login

判断当前行是否在事务中,返回true/false

.$pdo->setAttribute(属性名,属性值);

设置pdo对象的属性值;

举例:$pdo->setAttribute(PDO::ATTR_ERRMOOE,PDO:ERRMOOE_EXCEPTIOM)
Copy after login

推荐学习:《PHP视频教程

The above is the detailed content of What is PDO in PHP? What are the common ways to use pdo objects?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template