Home > Backend Development > PHP Tutorial > PHP implementation can be used for mysql, mssql, pg database operation class, mysqlmssql_PHP tutorial

PHP implementation can be used for mysql, mssql, pg database operation class, mysqlmssql_PHP tutorial

WBOY
Release: 2016-07-13 10:11:44
Original
867 people have browsed it

php implementation can be used for mysql, mssql, pg database operation class, mysqlmssql

The examples in this article describe the database operation classes that can be used in three databases: mysql, mssql, and pg. You can easily change the type of your database by making any modifications. Share it with everyone for your reference. The specific analysis is as follows:

Function list, index:

Open: Open database connection Line:71
Close: Close the database connection Line:107
SelectDB: Select database Line:129
Query:Create query Line:151
DataSeek: Move record pointer Line:175
FieldName: Get the field name Line:198
FieldType: Get the field type Line:220
FieldLenght: Get the field length Line:242
FetchRow: Get data and save to array (numeric index) Line:264
FetchArray: Get data and save it into an array (number and association) Line:289
FetchObject: Get data and save it to the object (object mode) Line:315
Result: Get the result data Line:341
FreeResult: Refresh the record set Line:363
RowsNumber: Get the number of records Line:385
FieldsNumber: Get the number of fields Line:407
CurRecNumber: Get the current record number (starting from 0) Line:429
RecordNumber: Get the current line number (starting from 1) Line:438
MoveFirstRec: Move to the first record Line:447
MoveLastRec: Move to the last record Line:469
MovePreviousRec: Move to the previous record Line:495
MoveNextRec: Move to the next record Line:521
MoveToRec: Move to a specific record (starting from 1) Line:548

php database operation code is as follows:

Copy code The code is as follows:
/***************************************************** ****************************
This class encapsulates database operations and has good portability. For databases: mysql, mssql, pg
*************************************************** **********************************
// - function list index:
// - Open: Open the database connection Line:71
// - Close: Close the database connection Line:107
// - SelectDB: Select database Line:129
// - Query: Create query Line:151
// - DataSeek: Move record pointer Line:175
// - FieldName: Get the field name Line:198
// - FieldType: Get the field type Line:220
// - FieldLenght: Get the field length Line:242
// - FetchRow: Get data and save to array (numeric index) Line:264
// - FetchArray: Get data and save it into an array (numeric and associative) Line:289
// - FetchObject: Get data and save it to the object (object mode) Line:315
// - Result: Get the result data Line:341
// - FreeResult: Refresh the record set Line:363
// - RowsNumber: Get the number of records Line:385
// - FieldsNumber: Get the number of fields Line:407
// - CurRecNumber: Get the current record number (starting from 0) Line:429
// - RecordNumber: Get the current line number (starting from 1) Line:438
// - MoveFirstRec: Move to the first record Line:447
// - MoveLastRec: Move to the last record Line:469
// - MovePreviousRec: Move to the previous record Line:495
// - MoveNextRec: Move to the next record Line:521
// - MoveToRec: Move to a specific record (starting from 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
{
/***********************************Member variable definition****************** *************************/
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
 
/***********************************Member method implementation****************** *************************/
/***************************************************** *********************************
*Function to connect to database
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Close database connection
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Select database
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Create query
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Move record pointer
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Get field name
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Get field type
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Get field length
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Get data and save it to an array, which can be accessed using numerical index
*************************************************** *************************************/
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;
}
 
/***************************************************** *********************************
*Get data and save it to an array, which can be accessed using numeric index and associative index
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Get data and save to object
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Get result data
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Release result data
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Get the number of records
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Get the number of fields
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Get the current record number (starting from 0)
*************************************************** *************************************/
Function CurRecNumber($result)
{
$r = $this->curRow[$result];
Return $r;
}
/***************************************************** *********************************
* Get the current line number (starting from 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;
}
/***************************************************** *********************************
*Move to last record
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Move to previous record
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Move to next record
*************************************************** *************************************/
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;
}
/***************************************************** *********************************
*Move to the specified record (number starts from 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;
}
}
//******************************The method is implemented****************** *************************//
?>

希望本文所述对大家的PHP数据库程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/927254.htmlTechArticlephp实现可用于mysql,mssql,pg数据库操作类,mysqlmssql 本文实例讲述了可用mysql,mssql,pg三种数据库的数据库操作类,你只要作任何修改就可以方便的...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template