PEAR::MDB2简单使用
PEAR::MDB2的简单使用,包含一个从Smarty Sample修改过来的例子
官方说明:
PEAR MDB2 is a merge of the PEAR DB and Metabase php database abstraction layers.
It provides a common API for all supported RDBMS. The main difference to most
other DB abstraction packages is that MDB2 goes much further to ensure
portability.
简单使用:
- 安装:
![]()
Code
#pear install MDB2
#pear install MDB2#mysql //必须安装,否则连接时错误提示not found。
#pear install MDB2#mysqli //形式为MDB2#$db - connect:
![]()
Code
require_once 'MDB2.php';
$dsn = array(
'phptype' => 'mysql',
'hostspec' => 'host',
'database' => 'db',
'username' => 'user',
'password' => 'password'
)
//$dsn = 'mysql://user:password@host/db';
//只调用factory()的时候它并没有去连接数据库,只有发送query请求的时候才会连接
$mdb2 =& MDB2::factory($dsn);
if (PEAR::isError($mdb2)) {
echo $mdb2->getMessage();
} - query
![]()
Code
$result = $mdb2->query($query); //$query为需要执行的sql语句 - fetch
![]()
Code
$data = $resutl->fetchRow($fetchmode);
//$fetchmode的默认值为MDB2_FETCHMODE_DEFAULT
//MDB2_FETCHMODE_DEFAULT
//MDB2_FETCHMODE_ORDERED //index下标的数组
//MDB2_FETCHMODE_ASSOC //散列形式的数组
//MDB2_FETCHMODE_ORDERED | MDB2_FETCHMODE_FLIPPED
//MDB2_FETCHMODE_ASSOC | MDB2_FETCHMODE_FLIPPED
//MDB2_FETCHMODE_OBJECT - 一个使用例子(Smarty Sample,经过修改)
![]()
Code
define('SQL_NONE', 1);
define('SQL_ALL', 2);
define('SQL_INIT', 3);
define('SQL_INDEX', 1);
define('SQL_ASSOC', 2);
class SQL {
var $db = null;
var $result = null;
var $error = null;
var $record = null;
function __construct() {
}
function connect($dsn) {
$this->db =& MDB2::factory($dsn);
// $this->db = & MDB2::connect($dsn);
if (PEAR::isError($this->db)) {
$this->error = $this->db->getMessage();
$this->logDBError($this->error,"failed to connect db.");
return false;
}
return true;
}
function disconnect() {
$this->db->disconnect();
}
/*string $query sql语句
*$type = SQL_NONE|SQL_INIT|SQL_ALL 不返回|返回一行|返回所有
*$format = SQL_INDEX|SQL_ASSOC 返回index下标数组|返回散列数组
*/
function query($query, $type = SQL_NONE, $format = SQL_INDEX) {
$this->record = array();
$_date = array();
$_fetchmode = ($format == SQL_ASSOC) ? MDB2_FETCHMODE_ASSOC : null;
$this->result = $this->db->query($query);
if (PEAR::isError($this->result)) {
$this->error = $this->result->getMessage();
$this->logDBError($this->error,"failed to get result.");
return false;
}
switch ($type) {
case SQL_ALL:
while($_row = $this->result->fetchRow($_fetchmode)) {
$_data[] = $_row;
}
$this->result->free();
$this->record = $_data;
break;
case SQL_INIT:
$this->record = $this->result->fetchRow($_fetchmode);
break;
case SQL_NONE:
default:
break;
}
return true;
}
function next($format = SQL_INDEX) {
$_fetchmode = ($format == SQL_ASSOC) ? MDB2_FETCHMODE_ASSOC : null;
if ($this->record = $this->result->fetchRow($_fetchmode)) {
return true;
}else {
$this->result->free();
return false;
}
}
/* 记录日志到/media/wind/www/logs/db_log,可自定义为/path/to/log
* $message 数据库错误信息
* string $info 自定义信息
*/
function logDBError($message,$info) {
$error = date("Y/m/d H:i:s") . "|"
. "MySQL|" . $_SERVER["PHP_SELF"] . "|"
. $info . "|" . $message . ".\n";
$fp = fopen("/media/wind/www/logs/db_log","a");
fwrite($fp,$error,strlen($error));
fclose($fp);
}
}
浙公网安备 33010602011771号