php pdo详解,php pdo函数库用法详解

在php中,pdo是一个“数据库访问抽象层”,作用是统一各种数据库的访问接口,与mysql和mysqli的函数库相比,pdo让跨数据库的使用更具有亲和力;

与adodb和mdb2相比,pdo更高效。

目前而言,实现“数据库抽象层”任重而道远,使用pdo这样的“数据库访问抽象层”是一个不错的选择。

php pdo中包含三个预定义的类,它们分别是 pdo、pdostatement 和 pdoexception。

一、pdo

代码示例:

pdo->begintransaction() — 标明回滚起始点

pdo->commit() — 标明回滚结束点,并执行sql

pdo->__construct() — 建立一个pdo链接数据库的实例

pdo->errorcode() — 获取错误码

pdo->errorinfo() — 获取错误的信息

pdo->exec() — 处理一条sql语句,并返回所影响的条目数

pdo->getattribute() — 获取一个“数据库连接对象”的属性

pdo->getavailabledrivers() — 获取有效的pdo驱动器名称

pdo->lastinsertid() — 获取写入的最后一条数据的主键值

pdo->prepare() — 生成一个“查询对象”

pdo->query() — 处理一条sql语句,并返回一个“pdostatement”

pdo->quote() — 为某个sql中的字符串添加引号

pdo->rollback() — 执行回滚

pdo->setattribute() — 为一个“数据库连接对象”设定属性

二、pdostatement

代码示例:

pdostatement->bindcolumn() — bind a column to a php variable

pdostatement->bindparam() — binds a parameter to the specified variable name

pdostatement->bindvalue() — binds a value to a parameter

pdostatement->closecursor() — closes the cursor, enabling the statement to be executed again.

pdostatement->columncount() — returns the number of columns in the result set

pdostatement->errorcode() — fetch the sqlstate associated with the last operation on the statement handle

pdostatement->errorinfo() — fetch extended error information associated with the last operation on the statement handle

pdostatement->execute() — executes a prepared statement

pdostatement->fetch() — fetches the next row from a result set

pdostatement->fetchall() — returns an array containing all of the result set rows

pdostatement->fetchcolumn() — returns a single column from the next row of a result set

pdostatement->fetchobject() — fetches the next row and returns it as an object.

pdostatement->getattribute() — retrieve a statement attribute

pdostatement->getcolumnmeta() — returns metadata for a column in a result set

pdostatement->nextrowset() — advances to the next rowset in a multi-rowset statement handle

pdostatement->rowcount() — returns the number of rows affected by the last sql statement

pdostatement->setattribute() — set a statement attribute

pdostatement->setfetchmode() — set the default fetch mode for this statement

详解1) pdo中的数据库连接

代码示例:

$dsn = ‘mysql:dbname=ent;host=127.0.0.1′;

$user = ‘root';

$password = ‘123456′;

try {

$dbh = new pdo($dsn, $user, $password, array(pdo::attr_persistent => true));

$dbh->query('set names utf8;');

foreach ($dbh->query('select * from tpm_juese') as $row) {

print_r($row);

}

} catch (pdoexception $e) {

echo ‘connection failed: ‘ . $e->getmessage();

}

许多web应用会因为使用了向数据库的持久连接而得到优化。持久连接不会在脚本结束时关闭,

相反它会被缓存起来并在另一个脚本通过同样的标识请求一个连接时得以重新利用。

持久连接的缓存可以使你避免在脚本每次需要与数据库对话时都要部署一个新的连接的资源消耗,让你的web应用更加快速。

上面实例中的array(pdo::attr_persistent => true)就是把连接类型设置为持久连接。

详解2) pdo中的事务

pdo->begintransaction(),pdo->commit(),pdo->rollback()这三个方法是在支持回滚功能时一起使用的。

pdo->begintransaction()方法标明起始点,pdo->commit()方法标明回滚结束点,并执行sql,pdo->rollback()执行回滚。

代码示例:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值