For the MySql database connection class, you can see the methods used by online veterans. you can use them directly, but only when you are familiar with them can you make some progress.
The code is as follows:
/*
* MySql database connection class
* Mysql. class. php 2009.04.15 by Hackbaby
*/
Class dbstuff {
Var $ version = '';
Var $ querynum = 0;
Var $ link = null;
// Connect to the database
Function connect ($ dbhost, $ dbuser, $ dbpw, $ dbname, $ pconnect = 0, $ halt = TRUE, $ dbcharset2 = ''){
$ Conn = empty ($ pconnect )? 'MySQL _ connect ': 'MySQL _ pconnect ';
If (! $ This-> link = @ $ conn ($ dbhost, $ dbuser, $ dbpw, 1 )){
$ Halt & $ this-> halt ('Can not connect to MySQL server ');
} Else {
If ($ this-> version ()> '4. 1 '){
Global $ charset, $ dbcharset;
$ Dbcharset = $ dbcharset2? $ Dbcharset2: $ dbcharset;
$ Dbcharset =! $ Dbcharset & in_array (strtolower ($ charset), array ('gbk', 'big5', 'utf-8 '))? Str_replace ('-', '', $ charset): $ dbcharset;
$ Serverset = $ dbcharset? 'Character _ set_connection = '. $ dbcharset.', character_set_results = '. $ dbcharset.', character_set_client = binary ':'';
$ Serverset. = $ this-> version ()> '5. 0.1 '? (Empty ($ serverset )? '': ','). 'SQL _ mode = \'\''):'';
$ Serverset & mysql_query ("SET $ serverset", $ this-> link );
}
$ Dbname & @ mysql_select_db ($ dbname, $ this-> link );
}
}
// Select a database
Function select_db ($ dbname ){
Return mysql_select_db ($ dbname, $ this-> link );
}
// Return method after Database execution
Function fetch_array ($ query, $ result_type = MYSQL_ASSOC ){
Return mysql_fetch_array ($ query, $ result_type );
}
// Return the returned value after the fetch_array method is executed
Function fetch_first ($ SQL ){
Return $ this-> fetch_array ($ this-> query ($ SQL ));
}
// Return the returned value after the result method is executed
Function result_first ($ SQL ){
Return $ this-> result ($ this-> query ($ SQL), 0 );
}
// Perform database operations
Function query ($ SQL, $ type = ''){
Global $ debug, $ discuz_starttime, $ sqldebug, $ sqlspenttimes;
// Determine debug debugging
If (defined ('sys _ debug') & SYS_DEBUG ){
@ Include_once WEB_ROOT. './include/debug. func. php ';
Sqldebug ($ SQL );
}
//
$ Func = $ type = 'unbuffered' & @ function_exists ('MySQL _ unbuffered_query ')?
'MySQL _ unbuffered_query ': 'MySQL _ query ';
If (! ($ Query = $ func ($ SQL, $ this-> link ))){
If (in_array ($ this-> errno (), array (2006,201 3) & substr ($ type, 0, 5 )! = 'Retry '){
$ This-> close ();
Require './config. inc. php ';
$ This-> connect ($ dbhost, $ dbuser, $ dbpw, $ dbname, $ pconnect, true, $ dbcharset );
$ This-> query ($ SQL, 'retry'. $ type );
} Elseif ($ type! = 'Silent' & substr ($ type, 5 )! = 'Silent '){
$ This-> halt ('MySQL Query error', $ SQL );
}
}
// Query the number of entries plus 1
$ This-> querynum ++;
Return $ query;
}
// Obtain the number of rows affected by the previous MySQL operation
Function affected_rows (){
Return mysql_affected_rows ($ this-> link );
}
// Database text error prompt
Function error (){
Return ($ this-> link )? Mysql_error ($ this-> link): mysql_error ());
}
// Returns the numeric encoding of the error message in the previous MySQL operation.
Function errno (){
Return intval ($ this-> link )? Mysql_errno ($ this-> link): mysql_errno ());
}
// Return database operation results
Function result ($ query, $ row = 0 ){
$ Query = @ mysql_result ($ query, $ row );
Return $ query;
}
// Return the number of operations such as database query
Function num_rows ($ query ){
$ Query = mysql_num_rows ($ query );
Return $ query;
}
// Obtain the number of fields in the result set.
Function num_fields ($ query ){
Return mysql_num_fields ($ query );
}
// Release the result memory
Function free_result ($ query ){
Return mysql_free_result ($ query );
}
// Obtain the ID generated by the previous INSERT operation
Function insert_id (){
Return ($ id = mysql_insert_id ($ this-> link)> = 0? $ Id: $ this-> result ($ this-> query ("SELECT last_insert_id ()"), 0 );
}
// Return the query result as an array
Function fetch_row ($ query ){
$ Query = mysql_fetch_row ($ query );
Return $ query;
}
// Retrieve the column information from the result set and return it as an object
Function fetch_fields ($ query ){
Return mysql_fetch_field ($ query );
}
// Obtain the Mysql version
Function version (){
If (empty ($ this-> version )){
$ This-> version = mysql_get_server_info ($ this-> link );
}
Return $ this-> version;
}
// Close the connection
Function close (){
Return mysql_close ($ this-> link );
}
// Error message
Function halt ($ message = '', $ SQL = ''){
Define ('cache _ FORBIDDEN ', TRUE );
Require_once WEB_ROOT. './include/db_mysql_error.inc.php ';
}
}
?>