Php implementation can be used in mysql, mssql, PostgreSQL database operation class, mysqlmssql_PHP tutorial

Source: Internet
Author: User
Tags php database
Php implementation can be used for mysql, mssql, PostgreSQL database operation class, and mysqlmssql. Php implementation can be used for mysql, mssql, and pg database operation classes. mysqlmssql this article describes the database operation classes available for mysql, mssql, and pg databases, you only need to make any modifications to facilitate php implementation for mysql, mssql, PostgreSQL database operations, and mysqlmssql.

The example in this article describes the database operation classes available for mysql, mssql, and pg databases. you can easily change the type of your database as long as you make any modifications. The specific analysis is as follows:

Function List, index:

Open: Open database connection Line: 71
Close: Close Database Connection Line: 107
SelectDB: Select database Line: 129
Query: create Query Line: 151
DataSeek: move the record pointer Line: 175
FieldName: obtain the field name Line: 198
FieldType: obtain the field type Line: 220
FieldLenght: get the field length Line: 242
FetchRow: get data and save it to an array (digital index) Line: 264
FetchArray: get the data and save it to the array (numbers and associations) Line: 289
FetchObject: get the data and save it to the object (object method) Line: 315
Result: obtain the Result data. Line: 341
FreeResult: Refresh record set Line: 363
RowsNumber: number of records retrieved Line: 385
FieldsNumber: Number of Retrieved Fields Line: 407
CurRecNumber: get the current record number (starting from 0) Line: 429
RecordNumber: get the current row number (starting from 1) Line: 438
MoveFirstRec: move to 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

The php database operation code is as follows:

The code is as follows:

<? PHP
/*************************************** **************************************** ***
This class encapsulates database operations and has good portability. for databases: mysql, mssql, and pg
**************************************** **************************************** ****
//-Function List index:
//-Open: Open the database connection Line: 71
//-Close: Close Database Connection Line: 107
//-SelectDB: Select database Line: 129
//-Query: Create a Query Line: 151
//-DataSeek: Move record pointer Line: 175
//-FieldName: obtains the field name Line: 198.
//-FieldType: obtains the field type Line: 220.
//-FieldLenght: get the field length Line: 242
//-FetchRow: get the data and save it to the array (digital index) Line: 264
//-FetchArray: get the data and save it to the array (numbers and associations) Line: 289
//-FetchObject: get data and save it to an object (object method) Line: 315
//-Result: get Result data Line: 341
//-FreeResult: Refresh record set Line: 363
//-RowsNumber: number of records retrieved Line: 385
//-FieldsNumber: Number of Retrieved Fields Line: 407
//-CurRecNumber: get the current record number (starting from 0) Line: 429
//-RecordNumber: get the current row 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; // Database type: mssql, mysql, and 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 data from DataSeek function array

/*********************************** Member method implementation * **************************************/
/*************************************** **************************************** *****
* Database connection functions
**************************************** **************************************** *****/
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 a 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;
}
/*************************************** **************************************** *****
* Obtain the 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;
}
/*************************************** **************************************** *****
* Obtain the 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;
}
/*************************************** **************************************** *****
* Obtain the 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 using 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;
}

/*************************************** **************************************** *****
* Obtain data and save it to an array. you can access the data by using a digital index or 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 the data and save it to the 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;
}
/*************************************** **************************************** *****
* Obtain 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;
}
/*************************************** **************************************** *****
* Obtain 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;
}
/*************************************** **************************************** *****
* Obtain 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 row 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 the 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 (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;
}
}
*** *************************************//
?>

I hope this article will help you design PHP database programs.

Examples in this article describes the database operation classes available for mysql, mssql, and pg databases. you can easily make any modifications...

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.