DB class for PHP

Source: Internet
Author: User
Tags pconnect

Cut from: http://www.oschina.net/code/snippet_96541_3441

DB class for PHP

<? Phpclass dB {private $ link_id; private $ handle; private $ is_log; private $ time; // constructor public function _ construct () {$ this-> 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 ["logf Ilepath "]. "dblog.txt", "A +"); $ this-> handle = $ handle;} // connect the database to 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 (! @ 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 ($ R Ow = & 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 be updated '); 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 ('condition not set for deletion '); 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 idpublic 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 ("Closed database connection "); 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 ("completes the entire Query Query task. The time used 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) ;}// get the number of milliseconds public function microtime_float () {list ($ USEC, $ Sec) = explode ("", microtime (); Return (float) $ USEC + (float) $ Sec) ;}}?>

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.