Home  >  Article  >  Backend Development  >  Using Zend_Db_Adapter to operate database_PHP tutorial

Using Zend_Db_Adapter to operate database_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:33:35928browse

Zend_Db_Adapter is the database abstraction layer API of zendframework. Based on pdo, you can use Zend_Db_Adapter to connect and process a variety of databases, including Microsoft SQL Server, MySql, SQLite, etc. To instantiate a Zend_Db_Adapter object for a different database, you need to statically call the Zend_Db::factory() method with the name of the adapter and the parameter array describing the database connection as parameters. For example, to connect to a local MySQL database with the database name "test" and the user name "root", you can perform the following operations:

<?php
require_once 'Zend/Db.php';
$params = array (	'host' => 'localhost',
					'username' => 'root',
					'password' => '',
					'dbname' => 'test'
				);
$db = Zend_Db::factory('PDO_MYSQL', $params);
?>

Query data directly

Once you get a Zend_Db_Adapter instance, you can directly execute sql statements for queries. Zend_Db_Adapter transmits these sql statements to the underlying PDO object, which combines and executes them. If there are query results, a PDOStatement object is returned for processing the results.

The database is as follows:

id name
1 A
2 B
3 C
4 D

PHP代码:

<?php
require_once 'Zend/Db.php';
$params = array (	'host' => 'localhost',
					'username' => 'root',
					'password' => '',
					'dbname' => 'test'
				);
$db = Zend_Db::factory('PDO_MYSQL', $params);
// 创建一个$db 对象,然后查询数据库
// 使用完整的sql 语句直接进行查询.
$sql = $db->quoteInto(
	'SELECT * FROM test WHERE id = ?',
	'3'
);
$result = $db->query($sql);
// 使用PDOStatement 对象$result 将所有结果数据放到一个数组中
$rows = $result->fetchAll();
?>
<pre class="brush:php;toolbar:false">
<?php
print_r($rows);
?>

程序运行结果:

Array
(
    [0] => Array
        (
            [id] => 3
            [name] => C
        )
)

你可以将数据自动的绑定到你的查询中。这意味着你在查询中可以设定多个指定的占位符,然后传送一个数组数据以代替这些占位符。这些替换的数据是自动进行加引号处理的,为防止数据库攻击提供了更强的安全性。

<?php
// 创建一个$db 对象,然后查询数据库.
// 这一次,使用绑定的占位符.
$result = $db->query(
	'SELECT * FROM example WHERE date > :placeholder',
	array('placeholder' => '2006-01-01')
);
// 使用PDOStatement 对象$result 将所有结果数据放到一个数组中
$rows = $result->fetchAll();
?>

或者,你也可以手工设置 sql 语句和绑定数据到 sql 语句。这一功能通过 prepare() 方法得到一个设定好的PDOStatement 对象,以便直接进行数据库操作。

<?php
// 创建一个$db 对象,然后查询数据库.
// 这次, 设定一个PDOStatement 对象进行手工绑定.
$stmt = $db->prepare('SELECT * FROM example WHERE date > :placeholder');
$stmt->bindValue('placeholder', '2006-01-01');
$stmt->execute();
// 使用PDOStatement 对象$result 将所有结果数据放到一个数组中
$rows = $stmt->fetchAll();
?>

事务处理

默认情况下,PDO(因此Zend_Db_Adapter 也是)是采用自动 commit 模式。也就是说,所有的数据库操作执行时就做了 commit 操作。假如你试图执行事务处理,最简单的是调用 beginTransaction() 方法,然后选择 commit 或者 rollback。之后,Zend_Db_Adapter 会回到自动 commit 模式下,直到你再次调用 beginTransaction() 方法。

<?php
// 创建一个$db 对象, 然后开始做一个事务处理.
$db->beginTransaction();
// 尝试数据库操作.
// 假如成功,commit 该操作;
// 假如, roll back.
try {
$db->query(...);
$db->commit();
} catch (Exception $e) {
$db->rollBack();
echo $e->getMessage();
}
?>

插入数据行

为了方便起见,你可以使用 insert() 方法将要插入的数据绑定并创建一个 insert 语句(绑定的数据是自动进行加引号处理以避免数据库攻击的)返回值并不是最后插入的数据的id,这样做的原因在于一些表并没有一个自增的字段;相反的,这个插入的返回值是改变的数据行数(通常情况为1)。假如你需要最后插入的数据id,可以在 insert 执行后调用lastInsertId()方法。

<?php//
// INSERT INTO round_table
// (noble_title, first_name, favorite_color)
// VALUES ("King", "Arthur", "blue");
//
// 创建一个$db 对象, 然后...
// 以"列名"=>"数据"的格式格式构造插入数组,插入数据行
$row = array (
'noble_title' => 'King',
'first_name' => 'Arthur',
'favorite_color' => 'blue',
);
// 插入数据的数据表
$table = 'round_table';
// i 插入数据行并返回行数
$rows_affected = $db->insert($table, $row);
$last_insert_id = $db->lastInsertId();
?>

更新数据行

为了方便起见,你可以使用 update() 方法确定需要 update 的数据并且创建一个 update 语句(确定的数据是自动加引号处理以避免数据库攻击的)。你可以提供一个可选的 where 语句说明 update 的条件(注意:where 语句并不是一个绑定参数,所以你需要自己数据进行加引号的操作)。

<?php
// UPDATE round_table
// SET favorite_color = "yellow"
// WHERE first_name = "Robin";
// 创建一个$db 对象, 然后...
// 以"列名"=>"数据"的格式构造更新数组,更新数据行
$set = array (
'favorite_color' => 'yellow',
);
// 更新的数据表
$table = 'round_table';
// where 语句
$where = $db->quoteInto('first_name = ?', 'Robin');
// 更新表数据,返回更新的行数
$rows_affected = $db->update($table, $set, $where);
?>

删除数据行

为了方便起见,你可以使用 delete() 方法创建一个 delete 语句;你也可以提供一个 where 语句以说明数据的删除条件。(注意:where 语句并不是一个绑定参数,所以你需要自己进行数据加引号处理)。

<?php
// 需要删除数据的表
// WHERE first_name = "Patsy";
// 创建一个$db 对象, 然后...
// 设定需要删除数据的表
$table = 'round_table';
// where 条件语句
$where = $db->quoteInto('first_name = ?', 'Patsy');
// 删除数据并得到影响的行数
$rows_affected = $db->delete($table, $where);
?>

取回查询结果

尽管你可以使用 query() 方法直接对数据库进行操作,但是通常情况下,仍然还是需要选择数据行并返回结果。以 fetch 开头的一系列的方法可以实现这个要求。对于每一种 fetch 系列的方法来说,你需要传送一个 select 的 sql 语句;假如你在操作语句中使用指定的占位符,你也可以传送一个绑定数据的数组对你的操作语句进行处理和替换。Fetch 系列的方法包括:

  • fetchAll()
  • fetchAssoc()
  • fetchCol()
  • fetchOne()
  • fetchPairs()
  • fetchRow()
<?php
// 创建一个$db 对象, 然后...
// 取回结果集中所有字段的值,作为连续数组返回
$result = $db->fetchAll(
"SELECT * FROM round_table WHERE noble_title = :title",
array('title' => 'Sir')
);
// 取回结果集中所有字段的值,作为关联数组返回
// 第一个字段作为码
$result = $db->fetchAssoc(
"SELECT * FROM round_table WHERE noble_title = :title",
array('title' => 'Sir')
);
// 取回所有结果行的第一个字段名
$result = $db->fetchCol(
"SELECT first_name FROM round_table WHERE noble_title = :title",
array('title' => 'Sir')
);
// 只取回第一个字段值
$result = $db->fetchOne(
"SELECT COUNT(*) FROM round_table WHERE noble_title = :title",
array('title' => 'Sir')
);
// 取回一个相关数组,第一个字段值为码
// 第二个字段为值
$result = $db->fetchPairs(
"SELECT first_name, favorite_color FROM round_table WHERE
noble_title = :title",
array('title' => 'Sir')
);
// 只取回结果集的第一行
$result = $db->fetchRow(
"SELECT * FROM round_table WHERE first_name = :name",
array('name' => 'Lancelot')
);
?>

添加引号防止数据库攻击

你应该处理将在 sql 语句中使用的条件值;这对于防止 sql 语句攻击是很有好处的。Zend_Db_Adapter (通过pdo)提供了两种方法帮助你手动的为条件值加上引号。第一种是 quote() 方法。该方法会根据数据库 adapter 为标量加上合适的引号;假如你试图对一个数组做 quote 操作,它将为数组中每个元素加上引号,并用","分隔返回。 (对于参数很多的函数来说,这点是很有帮助的)。

<?php
// 创建一个$db 对象,假设数据库adapter 为mysql.
// 为标量加引号
$value = $db->quote('St John"s Wort');
// $value 现在变成了'"St John\"s Wort"' (注意两边的引号)
// 为数组加引号
$value = $db->quote(array('a', 'b', 'c');
// $value 现在变成了'"a", "b", "c"' (","分隔的字符串)
?>

第二种是 quoteInto() 方法,你提供一个包含问号占位符的基础字符串,然后在该位置加入带引号的标量或者数组。该方法对于随需构建查询 sql 语句和条件语句是很有帮助的。使用 quoteInto 处理过的标量和数组返回结果与 quote() 方法相同。

<?php
// 创建一个$db 对象,假设数据库adapter 为mysql.
// 在where 语句中为标量加上引号
$where = $db->quoteInto('id = ?', 1);
// $where 现在为'id = "1"' (注意两边的引号)
// 在where 语句中为数组加上引号
$where = $db->quoteInto('id IN(?)', array(1, 2, 3));
// $where 现在为'id IN("1", "2", "3")' (一个逗号分隔的字符串)
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/752453.htmlTechArticleZend_Db_Adapter 是 zendframework 的数据库抽象层 API,基于 pdo,你可以使用 Zend_Db_Adapter 连接和处理多种数据库,包括 microsoft SQL Server,MySql,SQ...
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