search
HomeBackend DevelopmentPHP ProblemWhat is PDO in PHP? What are the common ways to use pdo objects?

我们学习了解了这么多关于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/返回一个真假值﹔

手册可以找到:

函数参考》数据库扩展》数据库抽象层>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);

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

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

结果:

成功:就是一个pdo结果集对象

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

结果: true﹔

其他操作:

. $pdo->lastInsertId) ;

获取最后添加的id值

. spdo->beginTransaction() ;

开启一个事务

. $pdo->commit();

提交一个事务

. spdo->rollBack() ;

回滚一个事务

. $pdo->inTransaction();

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

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

设置pdo对象的属性值;

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

推荐学习:《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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version