Package for database operations in PHP
Author: Joined: 2003-11-22 Views: 163 PHP in the package of database operations in a dynamic mesh design to be involved in many operations on the database, but sometimes with as needed and switch to other back-end database, you Need a lot of modifications. 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 name VAR $ dbusername; // Database User name VAR $ dbpassword; // User password // Sets host, username and password function Function setParameter ($ Host, $ username, $ password) { $ this-> dbusername = $ usrname; $ this-> dbhost = $ host; $ this-> dbpassword = $ password;} // Database function function dbconnect () {switch ($ this-> dbid) {casse 1 Return @MYSQL_CONNECT ($ this-> dbhost, $ this-> dbusername, $ this-> dbpassword); case 2; // with function of SQL Server Case 3; // Turn off the ODBC function}} // off Database function function dbclose ($ datahandle) {switch ($ this-> dbid) {case 1; mysql_close ($ dataHandle); case 2; // with function of SQL Server Case 3; // Support ODBC function} } // Execute SQL statement function Function DBQuery ($ DBNAME, $ SQL, $ DBHANDLE) {Switch ($ this-> dbid) {case 1; return @MYSQL_DB_QUERY ($ DBNAME, $ SQL, $ DBHANDLE); case 2; / / With a function of support SQL Server Case 3; // with a function of support ODBC}}} // Retrieve the current record function of SQL return value Function DBFETCHROW ($ DataHandle, $ Offset = -1) {Switch ($ this-> DBID) {Case 1; @Mysql_data_seek; return @Mysql_Fetch_row ($ dataHandle); case 2; // with a function of SQL Server Case 3; // Return to the ODBC}}} // Return Retrieval Record Multi-function Function DBNUMROWS ($ DataH Andle {switch ($ this-> dbid) {case 1; return @MYSQL_NUM_ROWS ($ dataHandle); case 2; // with function of SQL Server Case 3; // Return to the ODBC}} // Column number function function dbnumcols ($ datahandle) {switch ($ this-> dbid) {case 1; return @MYSQL_NUM_FIELDS ($ dataHandle); case 2; // with a function of SQL Server Case 3; // Support ODBC Function}}} The instructions are now used as follows: Use DbinterFace class in the program $ test = new dbinterface; 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 (); Return Value: FASLE, Database Connection Errors> 0, Database Connection Handle Database Off: Void Test-> DBClose (dbhandle); Table operating:
INT TEST-> DBQUERY (String DatabaseName, String SQL, DBHANDLE); Execute SQL statement Return Value: False, SQL Execute Errors> 0, SQL Performs correct, point to SQL return value, data operation: int test-> dbfetchrow (DataHandle, INT offset; retrieve 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 SQL execution (main Select statement) INT TEST-> DBNUMCOLS (DataHandle); After SQL execution (mainly SELECT statement), the number of record fields obtained now, now we send an example: 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 ----- Require ("testdb.inc"); // Loading DBINTERFACE $ test = new dbinterface; // Class Dbinterface generates an object $ test-> setParameter ("localhost", "root", "); // set database parameters $ db = $ test-> dbconnect (); // Connect database $ query =" Select Name, Pay from table "; // Set SQL statement $ TEMP_RESULT = $ TEST-> DBQUERY (" testdb ", $ query, $ db); // Execute data main library operation Echo" "; $ ls_num = $ test-> dbnumrows $ TEMP_RESULT); // Number of records of query results Echo $ Ls_Num; Echo "; if (ls_num> 0) {$ ls_col = $ test-> dbnumcols ($ dB); // Number of columns Echo $ LS_COL "echo" "; $ cate_result = $ test-> dbfetchrow ($ TEMP_RESULT, 0); // Get the first line of the record number $ hcid = $ cate_result [0]; // Get NAME value $ hcate = $ cate_result [ 1]; // Get the value of Pay $ Hcid; Echo "; Echo $ hcate;}?>
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. ">