欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP PDO函数库(PDO Functions)第1/2页

程序员文章站 2024-02-10 12:39:04
与adodb和mdb2相比,pdo更高效。目前而言,实现“数据库抽象层”任重而道远,使用pdo这样的“数据库访问抽象层”是一个不错的选择。 pdo->begintra...
与adodb和mdb2相比,pdo更高效。目前而言,实现“数据库抽象层”任重而道远,使用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->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
从函数列表可以看出,操作基于不同的对象,“pdo”表示的是一个数据库连接对象(new pdo产生),“pdostatement”表示的是一个查询对象(pdo->query()产生)或者是一个结果集对象(pdo->prepare()产生)。
一个“数据库连接对象”的例子,返回“pdo”:
复制代码 代码如下:

<?php
$dbh = new pdo('mysql:host=localhost;dbname=test', 'root', '');
?>

一个“查询对象”的例子,返回“pdostatement”:
复制代码 代码如下:

<?php
$sql = "insert into `test`.`table` (`name` ,`age`)values (?, ?);";
$stmt = $dbh->prepare($sql);
?>

一个“结果集对象”的例子,返回“pdostatement”:
复制代码 代码如下:

<?php
$sql = "select * from `table` where `name` = 'samon'";
$stmt = $dbh->query($sql);
?>

1