PHP implementation can be used to MYSQL,MSSQL,PG database operation class _php skills

Source: Internet
Author: User
Tags mssql php database

This example describes the database operations classes available in MYSQL,MSSQL,PG three databases, and you can easily change the type of your database as long as you make any changes. Share it for your reference. The specific analysis is as follows:

Function List, index:

Open: Opening database connection line:71
Close: Closes the database connection line:107
SELECTDB: Select Database line:129
Query: Creating Queries line:151
Dataseek: Move record pointer line:175
FieldName: Get field name line:198
FieldType: Get field type line:220
Fieldlenght: Get field length line:242
Fetchrow: Get the data and save it to an array (numeric index) line:264
Fetcharray: Get the data and save it in an array (numbers and associations) line:289
Fetchobject: Get the data and save it to the object (object mode) line:315
Result: Get Results data line:341
Freeresult: Refreshing recordsets line:363
Rowsnumber: Get the number of records line:385
Fieldsnumber: Get the number of fields line:407
Currecnumber: Gets the current record number (starting from 0) line:429
RecordNumber: Gets the current line number (starting from 1) line:438
Movefirstrec: Move to first record line:447
Movelastrec: Move to 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

The PHP database operation class code is as follows:

Copy Code code as follows:
? Php
/**********************************************************************************
This class encapsulates database operations with good portability for databases: MYSQL,MSSQL,PG
************************************************************************************
-Function Manifest Index:
-Open: Opening database connection line:71
-Close: Closes the database connection line:107
-SELECTDB: Select Database line:129
-Query: Create queries line:151
-Dataseek: Move record pointer line:175
-FieldName: Get field name line:198
-FieldType: Get field type line:220
-Fieldlenght: Get field length line:242
-Fetchrow: Get the data and save it to an array (numeric index) line:264
-Fetcharray: Get the data and save it in an array (numbers and associations) line:289
-Fetchobject: Get the data and save it to the object (object mode) line:315
-Result: Get Results data line:341
-Freeresult: Refreshing recordsets line:363
-Rowsnumber: Get the number of records line:385
-Fieldsnumber: Get the number of fields line:407
-Currecnumber: Gets the current record number (starting from 0) line:429
-recordnumber: Gets 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 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 to the result
Associated with the specified result identifier array
var $seek; Current row number of 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 digital 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;
}

/************************************************************************************
* Gets the data and saves it to the array, which can be accessed using the digital index and the 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 the 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 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 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 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 specified record (numbering starting 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 is complete ****************************************//
?>

I hope this article will help you with your PHP database program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.