通过 Pear DB可以从查询结果获得更多有用的数据信息 。这些方法有:
numRows(): 通过一个"SELECT" 查询返回所有数据的数量。
numCols():通过一个"SELECT" 查询返回所有的列。
affectedRows(): 通过("INSERT", "UPDATE" or "DELETE")操作返回所有受影响的数据行数。
tableInfo():通过一个"SELECT" 查询返回一个包含数据信息的数组。
可用方法列表:
numRows(): 通过一个"SELECT" 查询返回所有数据的数量。
numCols():通过一个"SELECT" 查询返回所有的列。
affectedRows(): 通过("INSERT", "UPDATE" or "DELETE")操作返回所有受影响的数据行数。
tableInfo():通过一个"SELECT" 查询返回一个包含数据信息的数组。
可用方法列表:
<?php
/*
* From the DB_(driver) objects
*/
// get the object with, ie:
$db = DB::connect('mysql://user:pass@localhost/my_db');
// Set options
$db->setErrorHandling();
$db->setFetchmode();
// Information
$db->affectedRows();
$db->tableInfo();
// Database manipulation
$db->query();
// Data fetch
$db->nextId();
$db->getOne();
$db->getRow();
$db->getCol();
$db->getAssoc();
$db->getAll();
// Place holders and execute related
$db->quote();
$db->prepare();
$db->execute();
$db->executeMultiple();
// Transactions
$db->autoCommit();
$db->commit();
$db->rollback();
// Disconnection
$db->disconnect();
/*
* From DB_result objects
*/
// get the object with, ie:
$res = $db->query('select * from foo');
// Data fetch
$res->fetchRow();
$res->fetchInto();
// Result Info
$res->numCols();
$res->numRows();
$res->tableInfo();
// Free
$res->free();
/*
* From DB_error objects
*/
// get the object with, ie:
$error = $db->query('select * from no_table');
$error->getMessage();
$error->getDebugInfo();
$error->toString();
?>
//
///其他详细使用
///
PEAR DB 的连接和断开
<?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();
?>
=======================
执行数据库并获得数据
<?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());
}
// 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];
}
?>
===============================
选择获取数据的格式<?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;
?>=============================
控制获取数据的数量<?php
...
// the row to start fetching
$from = 50;
// how many results per page
$res_per_page = 10;
// the last row to fetch for this page
$to = $from + $res_per_page;
foreach (range($from, $to) as $rownum) {
if (!$row = $res->fetchrow($fetchmode, $rownum)) {
break;
}
$id = $row[0];
....
}
?>
===========================
清楚结果释放变量
<?php
...
$result = $db->query('SELECT * FROM clients');
while ($row = $result->fetchRow()) {
...
}
$result->free();
?>
===========================