Encapsulation of database operations in PHP

Source: Internet
Author: User
The encapsulation of database operations in PHP. Read the encapsulation of database operations in PHP. the encapsulation of database operations in PHP involves many database operations in the dynamic network design, however, if you use other background databases as needed, you need to modify a large number of programs. This is a tedious, time-consuming and error-prone task. In fact, we can use the PHP class "> <LINKhref =" http: // www. php100 PHP to encapsulate database operations

In the dynamic network design, many operations on the database are involved. However, sometimes you need to change to another background database as needed, and a large number of programs need to be modified. This is a tedious, time-consuming and error-prone task. In fact, we can use PHP classes to encapsulate database operations, so that the written program can complete the changes to the background database with a small change.
Now we encapsulate it in dbfz. inc. Its design is as follows:

Class dbInterface {var $ dbID = 1; // The database used to determine the current operation. if dbID is 1, it indicates MySql. if dbID is 2, it indicates SQL Server. if dbID is 3, it indicates ODBC or other databases.
Var $ dbHost; // host domain name of the database
Var $ dbUsername; // database username
Var $ dbPassword; // user password
// Set host, user name, and password functions
Function setParameter ($ host, $ username, $ password ){
$ This-> dbUsername = $ username;
$ This-> dbHost = $ host;
$ This-> dbPassword = $ password;

} // Join database function
Function dbConnect (){
Switch ($ this-> dbID)
{
Case 1;
Return @ mysql_connect ($ this-> dbHost, $ this-> dbUsername, $ this-> dbPassword );
Case 2;
// Use functions that support SQL Server
Case 3;
// Use functions that support ODBC
}
}
// Close the database function
Function dbClose ($ dataHandle ){
Switch ($ this-> dbID)
{
Case 1;
Mysql_close ($ dataHandle );
Case 2;
// Use functions that support SQL Server
Case 3;
// Use functions that support ODBC
}
}

// Execute SQL statement functions
Function dbQuery ($ dbName, $ SQL, $ dbHandle ){
Switch ($ this-> dbID)
{
Case 1;
Return @ mysql_db_query ($ dbName, $ SQL, $ dbHandle );
Case 2;
// Use functions that support SQL Server
Case 3;
// Use functions that support ODBC
}
}

// Retrieves the current record function of the SQL return value
Function dbFetchrow ($ dataHandle, $ offset =-1 ){
Switch ($ this-> dbID)
{
Case 1;
@ Mysql_data_seek ($ dataHandle, $ offset );
Return @ mysql_fetch_row ($ dataHandle );
Case 2;
// Use functions that support SQL Server
Case 3;
// Use functions that support ODBC
}
}

// Return the number of retrieved records function
Function dbNumrows ($ dataHandle ){
Switch ($ this-> dbID)
{
Case 1;
Return @ mysql_num_rows ($ dataHandle );
Case 2;
// Use functions that support SQL Server
Case 3;
// Use functions that support ODBC
}
}

// Return the number of retrieved columns function
Function dbNumcols ($ dataHandle ){
Switch ($ this-> dbID)
{
Case 1;
Return @ mysql_num_fields ($ dataHandle );
Case 2;
// Use functions that support SQL Server
Case 3;
// Use functions that support ODBC
}
}
}

The usage instructions are as follows:

Generate an object $ test = new dbInterface using the dbInterface class in the program;

Set parameters
Test-> $ dbUsername; user name
Test-> $ dbPassword; password
Test-> $ dbHost; host
Void setParameter (string host, string username, string password );

Database Connection: dbhandle test-> dbConnect ();

Returned value: fasle, database connection error
> 0, database connection handle

Shut down the database: void test-> dbClose (dbhandle );

Table operation: int test-> dbQuery (string databasename, string SQL, dbhandle); execute the SQL statement

Returned value: false, SQL execution error
> 0. the SQL statement is correctly executed and points to the SQL return value,

Data operation: int test-> dbFetchrow (dataHandle, int offset); retrieves the current record of the SQL return value. after successful execution, the pointer moves to the next record.
Int test-> dbNumrows (dataHandle); number of records obtained after SQL execution (mainly SELECT statements)
Int test-> dbNumcols (dataHandle); number of record fields obtained after SQL execution (mainly SELECT statements)

Now we will give an example:

The database uses MQSQL: the host name is "localhost", and the username is "root" and Password "".
In mysql, there is a testdb database and its table table1. The table includes two fields: name and pay.

-----

Test

Require ("testdb. inc"); // load the dbInterface class
$ Test = new dbInterface; // use the class dbInterface to generate an object
$ Test-> setParameter ("localhost", "root", ""); // Set database parameters
$ Db = $ test-> dbConnect (); // connect to the database
$ Query = "SELECT name, pay FROM table"; // you can specify an SQL statement.
$ Temp_result = $ test-> dbQuery ("testdb", $ Query, $ db); // perform operations on the data Master Database
Echo"
";
$ Ls_num = $ test-> dbNumrows ($ temp_result); // number of records obtained from the query results
Echo $ ls_num;
Echo"
";
If (ls_num> 0)
{
$ Ls_col = $ test-> dbNumcols ($ db); // Obtain the number of columns in the table
Echo $ ls_col;
Echo"
";
$ Cate_result = $ test-> dbFetchrow ($ temp_result, 0); // obtain the first row of the number of records
$ Hcid = $ cate_result [0]; // Obtain the name value.
$ Hcate = $ cate_result [1]; // get the pay value
Echo $ hcid;
Echo"
";
Echo $ hcate;
}

?>



This is a simple application encapsulated class to complete database operations. To operate other databases, you only need to modify the dbID variable in the dbInterface class.

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.