Contribute a mysql connection operation class

Source: Internet
Author: User
Tags filetime pconnect
Mysql connection operation class on the phpmysql website/* obtain the time of the last updated table in the SQL statement. if there are multiple tables, return the time of the latest table */

Mysql connection operations on the php mysql website


 Cls_mysql ($ dbhost, $ dbuser, $ dbpw, $ dbname, $ charset, $ pconnect, $ quiet );
} Function cls_mysql ($ dbhost, $ dbuser, $ dbpw, $ dbname = "", $ charset = "gbk", $ pconnect = 0, $ quiet = 0) {if (defined ("SD_CHARSET") {$ charset = strtolower (str_replace ("-", "", SD_CHARSET);} if (defined ("ROOT_PATH ") &&! $ This-> root_path) {$ this-> root_path = ROOT_PATH;} if ($ quiet) {$ this-> connect ($ dbhost, $ dbuser, $ dbpw, $ dbname, $ charset, $ pconnect, $ quiet);} else {$ this-> settings = array ("dbhost" => $ dbhost, "dbuser" = $ dbuser, "dbpw" => $ dbpw, "dbname" => $ dbname, "charset" => $ charset, "pconnect" => $ pconnect );}} function connect ($ dbhost, $ dbuser, $ dbpw, $ dbname = "", $ charset = "utf8", $ pconnect = 0, $ quiet = 0) {if ($ pc Onnect) {if (! ($ This-> link_id = @ mysql_pconnect ($ dbhost, $ dbuser, $ dbpw) {if (! $ Quiet) {$ this-> ErrorMsg ("Can" t pConnect MySQL Server ($ dbhost )! ") ;}Return false ;}} else {if (PHP_VERSION >=" 4.2") {$ this-> link_id = @ mysql_connect ($ dbhost, $ dbuser, $ dbpw, true);} else {$ this-> link_id = @ mysql_connect ($ dbhost, $ dbuser, $ dbpw); mt_srand (double) microtime () * 1000000 ); // initialize the random number function for PHP versions earlier than 4.2} if (! $ This-> link_id) {if (! $ Quiet) {$ this-> ErrorMsg ("Can" t Connect MySQL Server ($ dbhost )! ");} Return false; }}$ this-> dbhash = md5 ($ this-> root_path. $ dbhost. $ dbuser. $ dbpw. $ dbname); $ this-> version = mysql_get_server_info ($ this-> link_id);/* If the mysql version is later than 4.1, you need to initialize the character set */if ($ this-> version> "4.1") {if ($ charset! = "Latin1") {mysql_query ("SET character_set_connection = $ charset, character_set_results = $ charset, character_set_client = binary", $ this-> link_id );} if ($ this-> version> "5.0.1") {mysql_query ("SET SQL _mode =", $ this-> link_id );}} $ sqlcache_config_file = $ this-> root_path. $ this-> cache_data_dir. "sqlcache_config_file _". $ this-> dbhash. ". php "; @ include ($ sqlcache_config_file); $ this-> starttime = time (); if ($ This-> max_cache_time & $ this-> starttime> $ this-> mysql_config_cache_file_time $ this-> max_cache_time) {if ($ dbhost! = ".") {$ Result = mysql_query ("show variables like" basedir "", $ this-> link_id); $ row = mysql_fetch_assoc ($ result); if (! Empty ($ row ["Value"] {1}) & $ row ["Value"] {1 }= ":"&&! Empty ($ row ["Value"] {2}) & $ row ["Value"] {2} = "\\") {$ this-> platform = "WINDOWS";} else {$ this-> platform = "OTHER" ;}} else {$ this-> platform = "WINDOWS ";} if ($ this-> platform = "OTHER" & ($ dbhost! = "." & Strtolower ($ dbhost )! = "Localhost: 3306" & $ dbhost! = "127.0.0.1: 3306") | (PHP_VERSION> = "5.1" & date_default_timezone_get () = "UTC") {$ result = mysql_query ("SELECT UNIX_TIMESTAMP () AS timeline, UNIX_TIMESTAMP ("". date ("Y-m-d H: I: s", $ this-> starttime ). "") AS timezone ", $ this-> link_id); $ row = mysql_fetch_assoc ($ result); if ($ dbhost! = "." & Strtolower ($ dbhost )! = "Localhost: 3306" & $ dbhost! = "127.0.0.1: 3306") {$ this-> timeline = $ this-> starttime-$ row ["timeline"];} if (PHP_VERSION >=" 5.1 "& date_default_timezone_get () =" UTC ") {$ this-> timezone = $ this-> starttime-$ row ["timezone"] ;}}$ content = "starttime. "; \ r \ n ". "$ this-> timeline = ". $ this-> timeline. "; \ r \ n ". "$ this-> timezone = ". $ this-> timezone. "; \ r \ n ". "$ this-> platform = ". """. $ this-> platform. ""; \ r \ n? ". ">"; @ File_put_contents ($ sqlcache_config_file, $ content);}/* Select database */if ($ dbname) {if (mysql_select_db ($ dbname, $ this-> link_id) === false) {if (! $ Quiet) {$ this-> ErrorMsg ("Can" t select MySQL database ($ dbname )! ");} Return false;} else {return true;} function select_database ($ dbname) {return mysql_select_db ($ dbname, $ this-> link_id);} function set_mysql_charset ($ charset) {/* If the mysql version is above 4.1, you need to initialize the character set */if ($ this-> version> "4.1") {if (in_array (strtolower ($ charset), array ("gbk", "big5 ", "UTF-8", "utf8") {$ charset = str_replace ("-", "", $ charset);} if ($ charset! = "Latin1") {mysql_query ("SET character_set_connection = $ charset, character_set_results = $ charset, character_set_client = binary", $ this-> link_id );}}} function fetch_array ($ query, $ result_type = MYSQL_ASSOC) {return mysql_fetch_array ($ query, $ result_type);} function query ($ SQL, $ type = "") {if ($ this-> link_id = NULL) {$ this-> connect ($ this-> settings ["dbhost"], $ this-> settings ["dbuser"], $ this-> settings ["dbp W "], $ this-> settings [" dbname "], $ this-> settings [" charset "], $ this-> settings [" pconnect "]); $ this-> settings = array ();} if ($ this-> queryCount queryLog [] = $ SQL;} if ($ this-> queryTime = "") {if (PHP_VERSION> = "5.0.0") {$ this-> queryTime = microtime (true);} else {$ this-> queryTime = microtime ();}} /* when the current time is greater than the class initialization time, the ping operation is automatically executed */if (PHP_VERSION> = "4.3" & time ()> $ this-> starttime 1) {mysql_ping ($ This-> link_id);} if (! ($ Query = mysql_query ($ SQL, $ this-> link_id) & $ type! = "SILENT") {$ this-> error_message [] ["message"] = "MySQL Query Error "; $ this-> error_message [] ["SQL"] = $ SQL; $ this-> error_message [] ["error"] = mysql_error ($ this-> link_id ); $ this-> error_message [] ["errno"] = mysql_errno ($ this-> link_id); $ this-> ErrorMsg (); return false ;} if (defined ("DEBUG_MODE") & (DEBUG_MODE & 8) = 8) {$ logfilename = $ this-> root_path. DATA_DIR. "/mysql_query _". $ this-> dbhash. "_". dat E ("Y_m_d "). ". log "; $ str = $ SQL. "\ n"; if (PHP_VERSION> = "5.0") {file_put_contents ($ logfilename, $ str, FILE_APPEND);} else {$ fp = @ fopen ($ logfilename, "AB"); if ($ fp) {fwrite ($ fp, $ str); fclose ($ fp) ;}}return $ query;} function affected_rows () {return mysql_affected_rows ($ this-> link_id);} function error () {return mysql_error ($ this-> link_id);} function errno () {return mysql_errno ($ this-> link_id);} function r Esult ($ query, $ row) {return @ mysql_result ($ query, $ row);} function num_rows ($ query) {return mysql_num_rows ($ query );} function num_fields ($ query) {return mysql_num_fields ($ query);} function free_result ($ query) {return mysql_free_result ($ query);} function insert_id () {return mysql_insert_id ($ this-> link_id);} function fetchRow ($ query) {return mysql_fetch_assoc ($ query);} function fetch_fields ($ query) {return mysql _ Fetch_field ($ query);} function version () {return $ this-> version;} function ping () {if (PHP_VERSION >=" 4.3 ") {return mysql_ping ($ this-> link_id) ;}else {return false ;}} function escape_string ($ unescaped_string) {if (PHP_VERSION> = "4.3 ") {return mysql_real_escape_string ($ unescaped_string);} else {return mysql_escape_string ($ unescaped_string);} function close () {return mysql_close ($ this-> link_id ); RMsg ($ message = "", $ SQL = "") {if (! File_exists (ROOT_PATH. "data/SQL _log") {mkdir (ROOT_PATH. "data/SQL _log");} if ($ message) {// echo"MySQL service error message: $ Message \ n "; include (ROOT_PATH. "/templates/default/500error.html");} else {include (ROOT_PATH. "/templates/default/500error.html"); $ msg = "MySQL service error message: \ r \ n ". print_r ($ this-> error_message, true); @ file_put_contents (ROOT_PATH. "data/SQL _log /". date ("Y-m-d-H-I-s", time ()). ". txt ", $ msg);} exit;}/* simulate the Adodb function */function selectLimit ($ SQL, $ num, $ start = 0) {if ($ start = 0) {$ SQL. = "LIMIT ". $ num;} else {$ SQL. = "LIMIT ". $ start. ",". $ num;} return $ this-> query ($ SQL);} function getOne ($ SQL, $ limited = false) {if ($ limited = true) {$ SQL = trim ($ SQL. "LIMIT 1") ;}$ res = $ this-> query ($ SQL); if ($ res! = False) {$ row = mysql_fetch_row ($ res); if ($ row! = False) {return $ row [0];} else {return "" ;}} else {return false ;}} function getOneCached ($ SQL, $ cached = "FILEFIRST") {$ SQL = trim ($ SQL. "LIMIT 1"); $ cachefirst = ($ cached = "FILEFIRST" | ($ cached = "MYSQLFIRST" & $ this-> platform! = "WINDOWS") & amp; $ this-> max_cache_time; if (! $ Cachefirst) {return $ this-> getOne ($ SQL, true);} else {$ result = $ this-> getSqlCacheData ($ SQL, $ cached ); if (empty ($ result ["storecache"]) = true) {return $ result ["data"] ;}}$ arr = $ this-> getOne ($ SQL, true); if ($ arr! = False & $ cachefirst) {$ this-> setSqlCacheData ($ result, $ arr);} return $ arr;} function getAll ($ SQL) {$ res = $ this-> query ($ SQL); if ($ res! = False) {$ arr = array (); while ($ row = mysql_fetch_assoc ($ res) {$ arr [] = $ row;} return $ arr ;} else {return false ;}} function getAllCached ($ SQL, $ cached = "FILEFIRST ") {$ cachefirst = ($ cached = "FILEFIRST" | ($ cached = "MYSQLFIRST" & $ this-> platform! = "WINDOWS") & amp; $ this-> max_cache_time; if (! $ Cachefirst) {return $ this-> getAll ($ SQL);} else {$ result = $ this-> getSqlCacheData ($ SQL, $ cached ); if (empty ($ result ["storecache"]) = true) {return $ result ["data"] ;}} $ arr = $ this-> getAll ($ SQL); if ($ arr! = False & $ cachefirst) {$ this-> setSqlCacheData ($ result, $ arr);} return $ arr;} function getRow ($ SQL, $ limited = false) {if ($ limited = true) {$ SQL = trim ($ SQL. "LIMIT 1") ;}$ res = $ this-> query ($ SQL); if ($ res! = False) {return mysql_fetch_assoc ($ res);} else {return false;} function getRowCached ($ SQL, $ cached = "FILEFIRST ") {$ SQL = trim ($ SQL. "LIMIT 1"); $ cachefirst = ($ cached = "FILEFIRST" | ($ cached = "MYSQLFIRST" & $ this-> platform! = "WINDOWS") & amp; $ this-> max_cache_time; if (! $ Cachefirst) {return $ this-> getRow ($ SQL, true);} else {$ result = $ this-> getSqlCacheData ($ SQL, $ cached ); if (empty ($ result ["storecache"]) = true) {return $ result ["data"] ;}}$ arr = $ this-> getRow ($ SQL, true); if ($ arr! = False & $ cachefirst) {$ this-> setSqlCacheData ($ result, $ arr);} return $ arr;} function getCol ($ SQL) {$ res = $ this-> query ($ SQL); if ($ res! = False) {$ arr = array (); while ($ row = mysql_fetch_row ($ res) {$ arr [] = $ row [0];} return $ arr;} else {return false;} function getColCached ($ SQL, $ cached = "FILEFIRST ") {$ cachefirst = ($ cached = "FILEFIRST" | ($ cached = "MYSQLFIRST" & $ this-> platform! = "WINDOWS") & amp; $ this-> max_cache_time; if (! $ Cachefirst) {return $ this-> getCol ($ SQL);} else {$ result = $ this-> getSqlCacheData ($ SQL, $ cached ); if (empty ($ result ["storecache"]) = true) {return $ result ["data"] ;}} $ arr = $ this-> getCol ($ SQL); if ($ arr! ==False & $ cachefirst) {$ this-> setSqlCacheData ($ result, $ arr);} return $ arr;} function autoExecute ($ table, $ field_values, $ mode = "INSERT", $ where = "", $ querymode = "") {$ field_names = $ this-> getCol ("DESC ". $ table); $ SQL = ""; if ($ mode = "INSERT") {$ fields = $ values = array (); foreach ($ field_names AS $ value) {if (array_key_exists ($ value, $ field_values) = true) {$ fields [] = $ value; $ values [] = """. $ Field_values [$ value]. "" ;}} if (! Empty ($ fields) {$ SQL = "INSERT ". $ table. "(". implode (",", $ fields ). ") VALUES (". implode (",", $ values ). ")" ;}} else {$ sets = array (); foreach ($ field_names AS $ value) {if (array_key_exists ($ value, $ field_values) = true) {$ sets [] = $ value. "= "". $ field_values [$ value]. ";}} if (! Empty ($ sets) {$ SQL = "UPDATE ". $ table. "SET ". implode (",", $ sets ). "WHERE ". $ where ;}} if ($ SQL) {return $ this-> query ($ SQL, $ querymode);} else {return false ;}} function autoReplace ($ table, $ field_values, $ update_values, $ where = "", $ querymode = "") {$ field_descs = $ this-> getAll ("DESC ". $ table); $ primary_keys = array (); foreach ($ field_descs AS $ value) {$ field_names [] = $ value ["Field"]; if ($ valu E ["Key"] = "PRI") {$ primary_keys [] = $ value ["Field"] ;}$ fields = $ values = array (); foreach ($ field_names AS $ value) {if (array_key_exists ($ value, $ field_values) = true) {$ fields [] = $ value; $ values [] = """. $ field_values [$ value]. ";}}$ sets = array (); foreach ($ update_values AS $ key =>$ value) {if (array_key_exists ($ key, $ field_values) = true) {if (is_int ($ value) | is_float ($ value) {$ sets [] = $ key. "= ". $ Key. "". $ value;} else {$ sets [] = $ key. "= "". $ value. ";}}$ SQL =" "; if (empty ($ primary_keys) {if (! Empty ($ fields) {$ SQL = "INSERT ". $ table. "(". implode (",", $ fields ). ") VALUES (". implode (",", $ values ). ")" ;}} else {if ($ this-> version ()> = "4.1") {if (! Empty ($ fields) {$ SQL = "INSERT ". $ table. "(". implode (",", $ fields ). ") VALUES (". implode (",", $ values ). ")"; if (! Empty ($ sets) {$ SQL. = "on duplicate key update ". implode (",", $ sets) ;}} else {if (empty ($ where) {$ where = array (); foreach ($ primary_keys AS $ value) {if (is_numeric ($ value) {$ where [] = $ value. "= ". $ field_values [$ value];} else {$ where [] = $ value. "= "". $ field_values [$ value]. ";}}$ where = implode (" AND ", $ where) ;}if ($ where &&(! Empty ($ sets) |! Empty ($ fields) {if (intval ($ this-> getOne ("select count (*) FROM $ table WHERE $ where")> 0) {if (! Empty ($ sets) {$ SQL = "UPDATE ". $ table. "SET ". implode (",", $ sets ). "WHERE ". $ where ;}} else {if (! Empty ($ fields) {$ SQL = "REPLACE ". $ table. "(". implode (",", $ fields ). ") VALUES (". implode (",", $ values ). ")" ;}}}}if ($ SQL) {return $ this-> query ($ SQL, $ querymode);} else {return false ;}} function setMaxCacheTime ($ second) {$ this-> max_cache_time = $ second;} function getMaxCacheTime () {return $ this-> max_cache_time;} function getSqlCacheData ($ SQL, $ cached = "") {$ SQL = trim ($ SQL); $ result = array (); $ result ["filename"] = $ this-> root_path. $ this-> cache_data_dir. "sqlcache _". abs (crc32 ($ this-> dbhash. $ SQL )). "_". md5 ($ this-> dbhash. $ SQL ). ". php "; $ data = @ file_get_contents ($ result [" filename "]); if (isset ($ data {23}) {$ filetime = substr ($ data, 13, 10); $ data = substr ($ data, 23); if ($ cached = "FILEFIRST" & time ()> $ filetime $ this-> max_cache_time) | ($ cached = "MYSQLFIRST" & $ this-> table_lastupdate ($ this-> get_table_name ($ SQL)> $ filetime )) {$ result ["storecache"] = true;} else {$ result ["data"] = @ unserialize ($ data ); if ($ result ["data"] === false) {$ result ["storecache"] = true ;} else {$ result ["storecache"] = false ;}} else {$ result ["storecache"] = true;} return $ result;} function setSqlCacheData ($ result, $ data) {if ($ result ["storecache"] === true & $ result ["filename"]) {@ file_put_contents ($ result ["filename"],"
 ". Time (). serialize ($ data); clearstatcache () ;}/ * obtain the time of the last updated table in the SQL statement. if there are multiple tables, returns the time of the latest table */function table_lastupdate ($ tables) {if ($ this-> link_id = NULL) {$ this-> connect ($ this-> settings ["dbhost"], $ this-> settings ["dbuser"], $ this-> settings ["dbpw"], $ this-> settings ["dbname"], $ this-> settings ["charset"], $ this-> settings ["pconnect"]); $ this-> settings = array ();} $ lastupdatetime = "0000-00-00 00:00:00 "; $ Tables = str_replace (" '"," ", $ tables); $ this-> mysql_disable_cache_tables = str_replace ("' "," ", $ this-> mysql_disable_cache_tables ); foreach ($ tables AS $ table) {if (in_array ($ table, $ this-> mysql_disable_cache_tables) = true) {$ lastupdatetime = "2037-12-31 23:59:59"; break ;} if (strstr ($ table ,". ")! = NULL) {$ tmp = explode (". ", $ table); $ SQL =" SHOW TABLE STATUS FROM '". trim ($ tmp [0]). "'Like "". trim ($ tmp [1]). ";} else {$ SQL =" SHOW TABLE STATUS LIKE "". trim ($ table ). ";}$ result = mysql_query ($ SQL, $ this-> link_id); $ row = mysql_fetch_assoc ($ result ); if ($ row ["Update_time"]> $ lastupdatetime) {$ lastupdatetime = $ row ["Update_time"] ;}}$ lastupdatetime = strtotime ($ lastupdatetime) -$ this-> tim Ezone $ this-> timeline; return $ lastupdatetime;} function get_table_name ($ query_item) {$ query_item = trim ($ query_item); $ table_names = array (); /* determine whether the statement contains JOIN */if (stristr ($ query_item, "JOIN") = "") {/* parse the general select from statement */if (preg_match ("/^ SELECT. *? FROM \ s *((? :'? \ W '? \ S * \. \ s *)? '? \ W '? (? :(?: \ S * )? \ S *'? \ W '?)? (?: \ S *, \ s *(? :'? \ W '? \ S * \. \ s *)? '? \ W '? (? :(?: \ S * )? \ S *'? \ W '?)?) *)/Is ", $ query_item, $ table_names) {$ table_names = preg_replace ("/((? :'? \ W '? \ S * \. \ s *)? '? \ W '?) [^,] */"," \ 1 ", $ table_names [1]); return preg_split ("/\ s *, \ s */", $ table_names) ;}} else {/* parses statements containing joins */if (preg_match ("/^ SELECT. *? FROM \ s *((? :'? \ W '? \ S * \. \ s *)? '? \ W '?) (? :(?: \ S * )? \ S *'? \ W '?)?. *? JOIN. * $/is ", $ query_item, $ table_names) {$ other_table_names = array (); preg_match_all ("/JOIN \ s *((? :'? \ W '? \ S * \. \ s *)? '? \ W '?) \ S */I ", $ query_item, $ other_table_names); return array_merge (array ($ table_names [1]), $ other_table_names [1]);} return $ table_names ;} /* Set the table that cannot be cached */function set_disable_cache_tables ($ tables) {if (! Is_array ($ tables) {$ tables = explode (",", $ tables);} foreach ($ tables AS $ table) {$ this-> mysql_disable_cache_tables [] = $ table;} array_unique ($ this-> mysql_disable_cache_tables) ;}}?>

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.