Many of the dynamic mesh design should involve the operation of the database, but sometimes you need a large number of modifications to use other background databases with the need. This is a boring, time-consuming and easy error. In fact, we can use the classes in PHP to implement the package of database operations, so that the procedures written can complete the changes to the background database under small changes.
Now we package it in dbfz.inc and its design is as follows:
Class dbinterface {var $ dbId = 1; // Used to determine the currently operated database, when DBID is 1 represents MySQL, when 2 represents SQL Server, ODBC or other.
Var $ dbhost; // The host domain where the database is located
Var $ dbusername; // Database Username
Var $ dbpassword; // user password
// Set the host, username and password function
Function SetParameter ($ HOST, $ Username, $ Password) {
$ this-> dbusername = $ usrname;
$ this-> dbhost = $ host;
$ this-> dbpassword = $ password;
} // Joint log function
Function dbconnect () {
Switch ($ this-> dbid)
{
Case 1;
Return @MYSQL_Connect ($ this-> Dbhost, $ this-> dbusername, $ this-> dbpassword);
Case 2;
// Functions that support SQL Server
Case 3;
// Function that supports ODBC
}
}
// Close the library function
Function dbclose ($ dataHandle) {
Switch ($ this-> dbid)
{
Case 1;
MySQL_Close ($ dataHandle);
Case 2;
// Functions that support SQL Server
Case 3;
// Function that supports ODBC
}
}
/ / Execute SQL statement functions
Function DBQuery ($ DBNAME, $ SQL, $ DBHANDLE) {
Switch ($ this-> dbid)
{
Case 1;
Return @MYSQL_DB_QUERY ($ DBNAME, $ SQL, $ DBHANDLE);
Case 2;
// Functions that support SQL Server
Case 3;
// Function that supports ODBC
}
}
// Retrieve 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;
// Functions that support SQL Server
Case 3;
// Function that supports ODBC
}
}
/ / Return to the retrieval record number function
Function DBNUMROWS ($ DataHandle) {
Switch ($ this-> dbid)
{
Case 1;
Return @mysql_num_rows ($ dataHandle);
Case 2;
// Functions that support SQL Server
Case 3;
// Function that supports ODBC
}
}
// Return to the search column function
Function dbnumcols ($ dataHandle) {
Switch ($ this-> dbid)
{
Case 1;
Return @mysql_num_fields ($ dataHandle);
Case 2;
// Functions that support SQL Server
Case 3;
// Function that supports ODBC
}
}
}
The instructions now use are as follows:
In the program, use the dbinterface class to born a target $ test = new dbinterface;
Setting parameters
TEST -> $ dbusername; user name
TEST -> $ dbpassword; password
TEST -> $ dbhost; host
Void setParameter (String Host, String Username, String Password);
Database connection: dbhandle test-> dbconnect ();
Return Value: FASLE, Database Connection Error
> 0, database connection handle
Database Close: Void Test-> DBClose (DBHANDLE);
Table Operation: int Test-> DBQuery (String DatabaseName, String SQL, DBHANDLE); Execute SQL statement
Return value: false, SQL execution error
> 0, SQL is correct, pointing to 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 shifts the next record
INT TEST-> DBNUMROWS (DataHandle); Number of records obtained after obtaining SQL execution (primarily for SELECT statement)
INT TEST-> DBNUMCOLS (DataHandle); Number of recorded records obtained after obtaining SQL execution (primarily for SELECT statement)
Now let's send an example of explanation:
The database uses MQSQL: its host name "localhost", the username "root" and password "".
There is a TESTDB database in MySQL and table table1, including: Name and PAY two fields
-----
HEAD>
PHP
Require ("testdb.inc"); // Load DBINTERFACE class
$ test = new dbinterface; // Generate an object with class DbinterFace
$ TEST-> SetParameter ("localhost", "root", ""); // set database parameters
$ DB = $ TEST-> dbconnect (); // Connect the database
$ Query = "SELECT NAME, PAY"; // Setting SQL statement
$ TEMP_RESULT = $ TEST-> DBQuery ("TestDB", $ Query, $ dB); // Execute Data Library Operation
echo "
"
$ ls_num = $ test-> dbnumrows ($ TEMP_RESULT); // Number of records to obtain query results
Echo $ LS_NUM;
echo "
"
IF (ls_num> 0)
{
$ ls_col = $ test-> dbnumcols ($ dB); // Number of columns to get the table
Echo $ ls_col;
echo "
"
$ CATE_RESULT = $ TEST-> DBFETCHROW ($ TEMP_RESULT, 0); // Take the first line of the number of records
$ hcid = $ cate_result [0]; // Get Name value $ hcate = $ cate_result [1]; // Get the value of PAY
Echo $ hcid;
echo "
"
Echo $ HCATE;
}
?>
Body> html>
This is a simple application package to complete the operation of the database. If you want to operate other databases, you only need to modify the DBID variable in the DbinterFace class.
Excerpt from: PHP Zhiyuan