程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php實現可用於mysql,mssql,pg數據庫操作類

php實現可用於mysql,mssql,pg數據庫操作類

編輯: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

php數據庫操作類代碼如下:
復制代碼 代碼如下:<?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數據庫程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved