Time = $ this-> microtime_float (); Require_once ("config. db. php "); $ This-> connect ($ db_config ["hostname"], $ db_config ["username"], $ db_config ["password"], $ db_config ["database"], $ db_config ["pconnect"]); $ This-> is_log = $ db_config ["log"]; If ($ this-> is_log ){ $ Handle = fopen ($ db_config ["logfilepath"]. "dblog.txt", "a + "); $ This-> handle = $ handle; } } // Database connection Public function connect ($ dbhost, $ dbuser, $ dbpw, $ dbname, $ pconnect = 0, $ charset = 'utf8 '){ If ($ pconnect = 0 ){ $ This-> link_id = @ mysql_connect ($ dbhost, $ dbuser, $ dbpw, true ); If (! $ This-> link_id ){ $ This-> halt ("database connection failed "); } } Else { $ This-> link_id = @ mysql_pconnect ($ dbhost, $ dbuser, $ dbpw ); If (! $ This-> link_id ){ $ This-> halt ("database persistent connection failed "); } } If (select_db ($ dbname, $ this-% 3Elink_id ">! @ Mysql_select_db ($ dbname, $ this-> link_id )){ $ This-> halt ('database selection failed '); } @ Mysql_query ("set names". $ charset ); } // Query Public function query ($ SQL ){ $ This-> write_log ("query". $ SQL ); $ Query = mysql_query ($ SQL, $ this-> link_id ); If (! $ Query) $ this-> halt ('query Error: '. $ SQL ); Return $ query; } // Obtain a record (MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH) Public function get_one ($ SQL, $ result_type = MYSQL_ASSOC ){ $ Query = $ this-> query ($ SQL ); $ Rt = & mysql_fetch_array ($ query, $ result_type ); $ This-> write_log ("get a record". $ SQL ); Return $ rt; } // Obtain all records Public function get_all ($ SQL, $ result_type = MYSQL_ASSOC ){ $ Query = $ this-> query ($ SQL ); $ I = 0; $ Rt = array (); While ($ row = & mysql_fetch_array ($ query, $ result_type )){ $ Rt [$ I] = $ row; $ I ++; } $ This-> write_log ("retrieve all records". $ SQL ); Return $ rt; } // Insert Public function insert ($ table, $ dataArray ){ $ Field = ""; $ Value = ""; If (! Is_array ($ dataArray) | count ($ dataArray) <= 0 ){ $ This-> halt ('no data to be inserted '); Return false; } While (list ($ key, $ val) = each ($ dataArray )){ $ Field. = "$ key ,"; $ Value. = "'$ val ',"; } $ Field = substr ($ field, 0,-1 ); $ Value = substr ($ value, 0,-1 ); $ SQL = "insert into $ table ($ field) values ($ value )"; $ This-> write_log ("insert". $ SQL ); If (! $ This-> query ($ SQL) return false; Return true; } // Update Public function update ($ table, $ dataArray, $ condition = ""){ If (! Is_array ($ dataArray) | count ($ dataArray) <= 0 ){ $ This-> halt ('no data to Update '); Return false; } $ Value = ""; While (list ($ key, $ val) = each ($ dataArray )) $ Value. = "$ key = '$ val ',"; $ Value. = substr ($ value, 0,-1 ); $ SQL = "update $ table set $ value where 1 = 1 and $ condition "; $ This-> write_log ("Update". $ SQL ); If (! $ This-> query ($ SQL) return false; Return true; } // Delete Public function delete ($ table, $ condition = ""){ If (empty ($ condition )){ $ This-> halt ('the condition for deletion not set '); Return false; } $ SQL = "delete from $ table where 1 = 1 and $ condition "; $ This-> write_log ("delete". $ SQL ); If (! $ This-> query ($ SQL) return false; Return true; } // Return result set Public function fetch_array ($ query, $ result_type = MYSQL_ASSOC ){ $ This-> write_log ("returned result set "); Return mysql_fetch_array ($ query, $ result_type ); } // Obtain the number of records Public function num_rows ($ results ){ If (! Is_bool ($ results )){ $ Num = mysql_num_rows ($ results ); $ This-> write_log ("the number of retrieved records is". $ num ); Return $ num; } Else { Return 0; } } // Release the result set Public function free_result (){ $ Void = func_get_args (); Foreach ($ void as $ query ){ If (is_resource ($ query) & get_resource_type ($ query) === 'mysql result '){ Return mysql_free_result ($ query ); } } $ This-> write_log ("Release result set "); } // Obtain the last inserted id Public function insert_id (){ $ Id = mysql_insert_id ($ this-> link_id ); $ This-> write_log ("the last inserted id is". $ id ); Return $ id; } // Close the database connection Protected function close (){ $ This-> write_log ("Database Connection closed "); Return @ mysql_close ($ this-> link_id ); } // Error message Private function halt ($ msg = ''){ $ Msg. = "\ r \ n". mysql_error (); $ This-> write_log ($ msg ); Die ($ msg ); } // Destructor Public function _ destruct (){ $ This-> free_result (); $ Use_time = ($ this-> microtime_float ()-($ this-> time ); $ This-> write_log ("the time used to complete the entire Query Task is". $ use_time ); If ($ this-> is_log ){ Fclose ($ this-> handle ); } } // Write the log file Public function write_log ($ msg = ''){ If ($ this-> is_log ){ $ Text = date ("Y-m-d H: I: s"). "". $ msg. "\ r \ n "; Fwrite ($ this-> handle, $ text ); } } // Obtain the number of milliseconds Public function microtime_float (){ List ($ usec, $ sec) = explode ("", microtime ()); Return (float) $ usec + (float) $ sec ); } } ?> |