php实现可用于mysql,mssql,pg数据库操作类
本文实例讲述了可用mysql,mssql,pg三种数据库的数据库操作类,你只要作任何修改就可以方便的改变你数据库的类型.分享给大家供大家参考。具体分析如下:
函数清单,索引:
open:打开数据库连接 line:71
close:关闭数据库连接 line:107
selectdb:选择数据库 line:129
query:创建查询 line:151
dataseek:移动记录指针 line:175
fieldname:获取字段名称 line:198
fieldtype:获取字段类型 line:220
fieldlenght:获取字段长度 line:242
fetchrow:获取数据并保存到数组(数字索引) line:264
fetcharray:获取数据并保存进数组(数字和关联) line:289
fetchobject:获取数据并保存到对象(对象方式) line:315
result:获取结果数据 line:341
freeresult:刷新记录集 line:363
rowsnumber:获取记录数量 line:385
fieldsnumber:获取字段数量 line:407
currecnumber:获取当前记录号(从0开始) line:429
recordnumber:获取当前行号(从1开始) line:438
movefirstrec:移动到第一条记录 line:447
movelastrec:移动到最后一条记录 line:469
movepreviousrec:移动到前一条记录 line:495
movenextrec:移动到下一条记录 line:521
movetorec:移动到一个特定的记录(从1开始) line:548
php数据库操作类代码如下:
/**********************************************************************************
此类将数据库操作封装,具有良好的可移植性,针对数据库:mysql,mssql,pg
************************************************************************************
// -函数清单 索引:
// - open: 打开数据库连接 line:71
// - close: 关闭数据库连接 line:107
// - selectdb: 选择数据库 line:129
// - query: 创建查询 line:151
// - dataseek: 移动记录指针 line:175
// - fieldname: 获取字段名称 line:198
// - fieldtype: 获取字段类型 line:220
// - fieldlenght: 获取字段长度 line:242
// - fetchrow: 获取数据并保存到数组(数字索引) line:264
// - fetcharray: 获取数据并保存进数组(数字和关联) line:289
// - fetchobject: 获取数据并保存到对象(对象方式) line:315
// - result: 获取结果数据 line:341
// - freeresult: 刷新记录集 line:363
// - rowsnumber: 获取记录数量 line:385
// - fieldsnumber: 获取字段数量 line:407
// - currecnumber: 获取当前记录号(从0开始) line:429
// - recordnumber: 获取当前行号(从1开始) line:438
// - movefirstrec: 移动到第一条记录 line:447
// - movelastrec: 移动到最后一条记录 line:469
// - movepreviousrec: 移动到前一条记录 line:495
// - movenextrec: 移动到下一条记录 line:521
// - movetorec: 移动到一个特定的记录(从1开始) line:548
************************************************************************************
//inputs:
// - dbtype: databases type: mssql, mysql, pg
// - connecttype: connection type: c - common connection,
// p - open persistent connection
// - connect: for ms sql server - server name,
// for mysql - hostname [:port] [:/path/to/socket] ,
// for postgresql - host, port, tty, options,
// dbname (without username and password)
// - username
// - password
// - dbname: database name
// - query: sql query
// - result: result set identifier
// - rownumber:
// - offset: field identifier
// - resulttype: a constant and can take the following values: pgsql_assoc, pgsql_num, and pgsql_both
// - fieldname
//
//returns:
// - result: result set identifier
// - connect link identifier
// - record number (starting at 0: currrecnumber or starting at 1: recordnumber)
// - number of fields in the specified result set
// - number of rows in the specified result set
*************************************************************************************/
class mdatabase
{
/***********************************成员变量定义***************************************/
var $dbtype; // 数据库类型: mssql, mysql, pg
var $connecttype; // 连接类型: c - common connection, p - open persistent connection
var $idcon; // 连接号
var $currow; // current row number of data from the result
// associated with the specified result identifier array
var $seek; // current row number of data from dataseek function array
/***********************************成员方法实现***************************************/
/************************************************************************************
*连接数据库的函数
*************************************************************************************/
function open($dbtype, $c, $connect, $username = "", $password = "")
{
$this->dbtype = $dbtype;
switch ($dbtype) {
case "mssql":
if ($connecttype == "c") {
$idcon = mssql_connect($connect, $username, $password);
} else {
$idcon = mssql_pconnect($connect, $username, $password);
}
break;
case "mysql":
if ($connecttype == "c") {
$idcon = mysql_connect($connect, $username, $password);
} else {
$idcon = mysql_pconnect($connect, $username, $password);
}
break;
case "pg":
if ($connecttype == "c") {
$idcon = pg_connect($connect . " user=" . $username . " password=" . $password);
} else {
$idcon = pg_pconnect($connect . " user=" . $username . " password=" . $password);
}
break;
default:
$idcon = 0;
break;
}
$this->idcon = $idcon;
return $idcon;
}
/************************************************************************************
*关闭数据库连接
*************************************************************************************/
function close()
{
switch ($this->dbtype) {
case "mssql":
$r = mssql_close($this->idcon);
break;
case "mysql":
$r = mysql_close($this->idcon);
break;
case "pg":
$r = pg_close($this->idcon);
break;
default:
$r = false;
break;
}
return $r;
}
/************************************************************************************
*选择数据库
*************************************************************************************/
function selectdb($dbname)
{
switch ($this->dbtype) {
case "mssql":
$r = mssql_select_db($dbname);
break;
case "mysql":
$r = mysql_select_db($dbname);
break;
case "pg":
$r = false;
break;
default:
$r = false;
break;
}
return $r;
}
/************************************************************************************
*创建查询
*************************************************************************************/
function query($query)
{
switch ($this->dbtype) {
case "mssql":
$r = mssql_query($query, $this->idcon);
break;
case "mysql":
$r = mysql_query($query, $this->idcon);
break;
case "pg":
$r = pg_exec($this->idcon, $query);
break;
default:
$r = false;
break;
}
$this->currow[$r] = 0;
$this->seek[$r] = 0;
return $r;
}
/************************************************************************************
*移动记录指针
*************************************************************************************/
function dataseek($result, $rownumber)
{
switch ($this->dbtype) {
case "mssql":
$r = mssql_data_seek($result, $rownumber);
break;
case "mysql":
$r = mysql_data_seek($result, $rownumber);
break;
case "pg":
$r = false;
break;
default:
$r = false;
break;
}
$this->seek[$result] = (int) $rownumber;
return $r;
}
/************************************************************************************
*获取字段名
*************************************************************************************/
function fieldname($result, $offset)
{
switch ($this->dbtype) {
case "mssql":
$r = mssql_field_name($result, $offset);
break;
case "mysql":
$r = mysql_field_name($result, $offset);
break;
case "pg":
$r = pg_fieldname($result, $offset);
break;
default:
$r = false;
break;
}
return $r;
}
/************************************************************************************
*获取字段类型
*************************************************************************************/
function fieldtype($result, $offset)
{
switch ($this->dbtype) {
case "mssql":
$r = mssql_field_type($result, $offset);
break;
case "mysql":
$r = mysql_field_type($result, $offset);
break;
case "pg":
$r = pg_fieldtype($result, $offset);
break;
default:
$r = false;
break;
}
return $r;
}
/************************************************************************************
*获取字段长度
*************************************************************************************/
function fieldlength($result, $offset)
{
switch ($this->dbtype) {
case "mssql":
$r = mssql_field_length($result, $offset);
break;
case "mysql":
$r = mysql_field_len($result, $offset);
break;
case "pg":
$r = pg_fieldsize($result, $offset);
break;
default:
$r = false;
break;
}
return $r;
}
/************************************************************************************
*获取数据并保存到数组,可以用数字索引方式访问数组
*************************************************************************************/
function fetchrow($result, $rownumber = 0)
{
switch ($this->dbtype) {
case "mssql":
$r = mssql_fetch_row($result);
break;
case "mysql":
$r = mysql_fetch_row($result);
break;
case "pg":
$r = pg_fetch_row($result, $rownumber);
if ($r) {
$this->currow[$result] = $rownumber;
$this->seek[$result] = $rownumber;
}
break;
default:
$r = false;
break;
}
return $r;
}
/************************************************************************************
*获取数据并保存到数组,可以用数字索引和关联索引的方式访问
*************************************************************************************/
function fetcharray($result, $rownumber = 0, $resulttype = 2)
{
switch ($this->dbtype) {
case "mssql":
$r = mssql_fetch_array($result);
break;
case "mysql":
$r = mysql_fetch_array($result);
break;
case "pg":
$r = pg_fetch_array($result, $rownumber, $resulttype);
if ($r) {
$this->currow[$result] = $rownumber;
$this->seek[$result] = $rownumber;
}
break;
default:
$r = false;
break;
}
return $r;
}
/************************************************************************************
*获取数据并保存到对象
*************************************************************************************/
function fetchobject($result, $rownumber = 0, $resulttype = 2)
{
switch ($this->dbtype) {
case "mssql":
$r = mssql_fetch_object($result);
break;
case "mysql":
$r = mysql_fetch_object($result);
break;
case "pg":
$r = pg_fetch_object($result, $rownumber, $resulttype);
if ($r) {
$this->currow[$result] = $rownumber;
$this->seek[$result] = $rownumber;
}
break;
default:
$r = false;
break;
}
return $r;
}
/************************************************************************************
*获取结果数据
*************************************************************************************/
function result($result, $rownumber, $fieldname)
{
switch ($this->dbtype) {
case "mssql":
$r = mssql_result($result, $rownumber, $fieldname);
break;
case "mysql":
$r = mysql_result($result, $rownumber, $fieldname);
break;
case "pg":
$r = pg_result($result, $rownumber, $fieldname);
break;
default:
$r = false;
break;
}
return $r;
}
/************************************************************************************
*释放结果数据
*************************************************************************************/
function freeresult($result)
{
switch ($this->dbtype) {
case "mssql":
$r = mssql_free_result($result);
break;
case "mysql":
$r = mysql_free_result($result);
break;
case "pg":
$r = pg_freeresult($result);
break;
default:
$r = false;
break;
}
return $r;
}
/************************************************************************************
*获取记录数量
*************************************************************************************/
function rowsnumber($result)
{
switch ($this->dbtype) {
case "mssql":
$r = mssql_num_rows($result);
break;
case "mysql":
$r = mysql_num_rows($result);
break;
case "pg":
$r = pg_numrows($result);
break;
default:
$r = false;
break;
}
return $r;
}
/************************************************************************************
*获取字段数量
*************************************************************************************/
function fieldsnumber($result)
{
switch ($this->dbtype) {
case "mssql":
$r = mssql_num_fields($result);
break;
case "mysql":
$r = mysql_num_fields($result);
break;
case "pg":
$r = pg_numfields($result);
break;
default:
$r = false;
break;
}
return $r;
}
/************************************************************************************
*获取当前记录号(从0开始)
*************************************************************************************/
function currecnumber($result)
{
$r = $this->currow[$result];
return $r;
}
/************************************************************************************
*获取当前行号(从1开始)
*************************************************************************************/
function recordnumber($result)
{
$cr = $this->currecnumber($result) + 1;
return $cr;
}
/************************************************************************************
*移动到第一条记录
*************************************************************************************/
function movefirstrec($result)
{
switch ($this->dbtype) {
case "pg":
$r = $this->fetchrow($result, 0);
break;
default:
$rn = $this->dataseek($result, 0);
if ($rn) {
$r = $this->fetchrow($result);
if ($r) $this->currow[$result] = $this->seek[$result];
} else {
$r = false;
}
break;
}
return $r;
}
/************************************************************************************
*移动到最后一条记录
*************************************************************************************/
function movelastrec($result)
{
$rs = $this->rowsnumber($result);
if ($rs) {
$rs--;
switch ($this->dbtype) {
case "pg":
$r = $this->fetchrow($result, $rs);
break;
default:
$rn = $this->dataseek($result, $rs);
if ($rn) {
$r = $this->fetchrow($result);
if ($r) $this->currow[$result] = $this->seek[$result];
} else {
$r = false;
}
break;
}
}
return $r;
}
/************************************************************************************
*移动到前一条记录
*************************************************************************************/
function movepreviousrec($result)
{
$rs = $this->currecnumber($result);
if ($rs) {
$rs--;
switch ($this->dbtype) {
case "pg":
$r = $this->fetchrow($result, $rs);
break;
default:
$rn = $this->dataseek($result, $rs);
if ($rn) {
$r = $this->fetchrow($result);
if ($r) $this->currow[$result] = $this->seek[$result];
} else {
$r = false;
}
break;
}
}
return $r;
}
/************************************************************************************
*移动到下一条记录
*************************************************************************************/
function movenextrec($result)
{
$rs = $this->currecnumber($result);
$rn = $this->rowsnumber($result);
$rs++;
if ($rs != $rn) {
switch ($this->dbtype) {
case "pg":
$r = $this->fetchrow($result, $rs);
break;
default:
$re = $this->fetchrow($result);
if ($re) {
$r = $re;
$this->currow[$result]++;
$this->seek[$result] = $this->currow[$result];
} else {
$r = false;
}
break;
}
}
return $r;
}
/************************************************************************************
*移动到指定记录(编号从0开始)
*************************************************************************************/
function movetorec($result, $rownumber)
{
$rn = $this->rowsnumber($result);
if ($rownumber > 0 and $rownumber < $rn) {
$rownumber--;
switch ($this->dbtype) {
case "pg":
$r = $this->fetchrow($result, $rownumber);
break;
default:
$rn = $this->dataseek($result, $rownumber);
if ($rn) {
$r = $this->fetchrow($result);
if ($r) $this->currow[$result] = $this->seek[$result];
} else {
$r = false;
}
break;
}
}
return $r;
}
}
//********************************方法实现完毕****************************************//
?>
希望本文所述对大家的php数据库程序设计有所帮助。