PHP implementations are available for MYSQL,MSSQL,PG database operations classes, Mysqlmssql
This paper describes the database operation classes available MYSQL,MSSQL,PG three databases, and you can easily change the type of your database if you make any changes. Share it for everyone's reference. The specific analysis is as follows:
Function List, index:
Open: Opening the database connection line:71
Close: Closes the database connection line:107
SELECTDB: Select Database line:129
Query: Create Queries line:151
Dataseek: Move the record pointer line:175
FieldName: Gets the field name line:198
FieldType: Gets the field type line:220
Fieldlenght: Gets the field length line:242
Fetchrow: Gets the data and saves it to an array (numeric index) line:264
Fetcharray: Get data and save into arrays (numbers and associations) line:289
Fetchobject: Get data and save to object (object mode) line:315
Result: Get Results data line:341
Freeresult: Refreshing the recordset line:363
Rowsnumber: Gets the number of records line:385
Fieldsnumber: Gets the number of fields line:407
Currecnumber: Gets the current record number (starting at 0) line:429
RecordNumber: Gets the current line number (starting at 1) line:438
Movefirstrec: Move to the first record line:447
Movelastrec: Move to the last record line:469
Movepreviousrec: Move to 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 class code is as follows:
Copy CodeThe code is as follows: <? Php
/**********************************************************************************
This class encapsulates database operations with good portability against the database: MYSQL,MSSQL,PG
************************************************************************************
-Function List index:
-Open: Opens the database connection line:71
-Close: Close database connection line:107
-SELECTDB: Select Database line:129
-Query: Create queries line:151
-Dataseek: Move record pointer line:175
-FieldName: Gets the field name line:198
-FieldType: Gets the field type line:220
-Fieldlenght: Gets the field length line:242
-Fetchrow: Get data and save to Array (numeric index) line:264
-Fetcharray: Get data and save into array (numbers and associations) line:289
-Fetchobject: Get data and save to object (object mode) line:315
-Result: Get Results data line:341
-Freeresult: Refresh record set line:363
-Rowsnumber: Gets the number of records line:385
-Fieldsnumber: Gets the number of fields line:407
-Currecnumber: Gets the current record number (starting at 0) line:429
-recordnumber: Gets the current line number (starting at 1) line:438
-Movefirstrec: Move to the first record line:447
-Movelastrec: Move to the last record line:469
-Movepreviousrec: Move to previous record line:495
-Movenextrec: Move to 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 [:p ort] [:/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; Database type: MSSQL, MySQL, pg
var $connectType; Connection type: C-common connection, P-open persistent connection
var $idCon; Connection number
var $curRow; Current row number of data from the result
Associated with the specified result identifier array
var $seek; Current row number of the data from Dataseek function array
/*********************************** Member method Implementation ***************************************/
/************************************************************************************
* Functions to connect to the 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 a 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 the data and save it to an array, you can access the array with a numeric 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 the data and save it to an array, which can be accessed with a numeric index and an associated 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;
}
/************************************************************************************
* Move to the first record
*************************************************************************************/
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 the 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 the 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 (numbering 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;
}
}
Method implementation Complete ****************************************//
?>
I hope this article is helpful to the PHP database design.
http://www.bkjia.com/PHPjc/927254.html www.bkjia.com true http://www.bkjia.com/PHPjc/927254.html techarticle PHP Implementation can be used for MYSQL,MSSQL,PG database operation class, Mysqlmssql This article describes the available MYSQL,MSSQL,PG three kinds of database operation class, you can only make any changes to the convenience of ...