Pear DB 入门指南

3.4 快速retrieve数据
当你不再想用fetchRow()方法来获取数据的时候,Pear DB通过sql语句提供一些特别的方法来返回想要的数据。这些方法有:getOne, getRow, getCol, getAssoc and getAll. 这有一些使用示例:

<?php
 // The pear base directory must be in your include_path
 require_once 'DB.php';
 $user = 'foo';
 $pass = 'bar';
 $host = 'localhost';
 $db_name = 'clients_db';
 
 // Data Source Name: This is the universal connection string
 $dsn = "mysql://$user:$pass@$host/$db_name";
 
 // DB::connect will return a Pear DB object on success
 // or a Pear DB Error object on error
 // You can also set to TRUE the second param
 // if you want a persistent connection:
 // $db = DB::connect($dsn, true);
 $db = DB::connect($dsn);
 
 // With DB::isError you can differentiate between an error or
 // a valid connection.
 if (DB::isError($db)) {
         die ($db->getMessage());
 }
 ....
 // You can disconnect from the database with:
 $db->disconnect();
 ?>

“get*() 系列方法” 可以为你做很多事情, 包括: 发起一个查询, 获取数据和清除结果。请注意所有的Pear DB函数将可能返回一个 Pear DB_error 对象。
3.5 从查询结果获得更多信息(numRows, numCols, affectedRows, tableInfo)
通过 Pear DB可以从查询结果获得更多有用的数据信息 。这些方法有:

numRows(): 通过一个”SELECT” 查询返回所有数据的数量。
numCols():通过一个”SELECT” 查询返回所有的列。
affectedRows(): 通过(“INSERT”, “UPDATE” or “DELETE”)操作返回所有受影响的数据行数。
tableInfo():通过一个”SELECT” 查询返回一个包含数据信息的数组。

示例:

<?php
 // Once you have a valid DB object
 ...
 $sql = "select * from clients";
 // If the query is a "SELECT", $db->query will return
 // a DB Result object on success.
 // Else it simply will return a DB_OK
 // On failure it will return a DB Error object.
 $result = $db->query($sql);
 // Always check that $result is not an error
 if (DB::isError($result)) {
         die ($result->getMessage());
 }
 ....
 ?>

3.6 自动增长(Sequences)
Sequences 为数据行提供独一无二的ID标识。如果熟悉MySQL之类的话,可以把它想象为AUTO_INCREMENT.它非常简单,首先你获取一个ID,然后在这个ID所在的行插入你所需要记录的数据。可以为你的表设置更多的Sequences,只需要保证在任何特殊的表中都使用一样的sequence就行。

<?php
 // Once you have a valid DB Result object
 ...
 // Get each row of data on each iteration until
 // there is no more rows
 while ($row = $result->fetchRow()) {
     $id = $row[0];
 }
 ?>

3.7 Prepare & Execute/ExcuteMultiple

<?php
 ...
 while ($result->fetchInto($row)) {
     $id = $row[0];
 }
 ?>

3.8 autoCommit, commit and rollback

<?php
 $res = $db->query('select id, name, email from users');
 $row = $res->fetchRow($mode);
 
 //With $mode = DB_FETCHMODE_ORDERED
 //The default behavior is to return an ordered array.
 $row = array (
     0 => <column "id" data>,
     1 => <column "name" data>,
     2 => <column "email" data>
 );
 
 $id = $row[0];
 
 //With $mode = DB_FETCHMODE_ASSOC
 //Returns an associative array with column names as array keys:
 $row = array (
     'id'    => <column "id" data>,
     'name'  => <column "name" data>,
     'email' => <column "email" data>
 );
 
 $id = $row['id'];
 
 //With $mode = DB_FETCHMODE_OBJECT
 //Returns a DB_row object with column names as properties:
 $row = db_row Object 
 (
     [id]    => <column "id" data>,
     [name]  => <column "name" data>,
     [email] => <column "email" data>
 )
 
 $id = $row->id;
 ?>

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据