This article introduces the mysql database operation class implemented by php, which is compatible with php4 and php5 and is worth learning. For more information, see. Share a php and mysql database class, which is well compatible with php4 and php5. The code is as follows:
Id = false; $ this-> Mode = 1; // 0 = silent, 1 = normal, 2 = debug, 3 = trace // Try to connect when instance is created if ($ srv! = '') $ This-> Connect ($ srv, $ uid, $ pwd, $ db, $ drv); $ this-> _ Dbs_Prepare (); // Hook for the TinyButStrong Template Engine $ GLOBALS ['_ TBS_UserFctLst'] ['k: tbssql'] = array ('type' => 4, 'Open' => array (& $ this, '_ Dbs_RsOpen'), 'fetch' => array (& $ this, '_ Dbs_RsFetch '), 'close' => array (& $ this, '_ Dbs_RsClose');} // Public methods function Connect ($ srv, $ uid, $ pwd, $ db, $ drv = '') {$ this-> Id = $ this-> _ Dbs_Connect ($ srv, $ uid, $ pwd, $ Db, $ drv); if ($ this-> Id = false) return $ this-> _ SqlError (false); return true;} function Close () {if ($ this-> Id! = False) $ this-> _ Dbs_Close ();} function Execute ($ SQL) {$ ArgLst = func_get_args (); $ this-> _ SqlProtect ($ SQL, $ ArgLst, 1); $ RsId = $ this-> _ Dbs_RsOpen (null, $ SQL); if ($ RsId = false) return $ this-> _ SqlError ($ this-> Id); $ this-> _ Dbs_RsClose ($ RsId); return true;} function Value ($ DefVal, $ SQL) {$ ArgLst = func_get_args (); $ this-> _ SqlProtect ($ SQL, $ ArgLst, 2); $ RsId = $ this-> _ Dbs_RsOpen (null, $ SQL ); if ($ RsId = = False) return $ this-> _ SqlError ($ this-> Id); $ Row = $ this-> _ Dbs_RsFetch ($ RsId ); if ($ Row = false) {$ x = $ DefVal;} else {$ x = reset ($ Row) ;}$ this-> _ Dbs_RsClose ($ RsId ); return $ x;} function Row1 ($ SQL) {$ ArgLst = func_get_args (); $ this-> _ SqlProtect ($ SQL, $ ArgLst, 1 ); $ RsId = $ this-> _ Dbs_RsOpen (null, $ SQL); if ($ RsId = false) return $ this-> _ SqlError ($ this-> Id ); $ x = $ this-> _ Dbs_RsFetch ($ RsId); $ this -> _ Dbs_RsClose ($ RsId); return $ x;} function Rows ($ SQL) {$ ArgLst = func_get_args (); $ this-> _ SqlProtect ($ SQL, $ ArgLst, 1); $ RsId = $ this-> _ Dbs_RsOpen (null, $ SQL); if ($ RsId = false) return $ this-> _ SqlError ($ this-> Id); $ x = array (); while ($ r = $ this-> _ Dbs_RsFetch ($ RsId )) {$ x [] = $ r ;}$ this-> _ Dbs_RsClose ($ RsId); return $ x ;} function AffectedRows () {return $ this-> _ Dbs_AffectedRows ();} function Las TRowId () {return $ this-> _ Dbs_LastRowId ();} // Private methods function _ SqlError ($ ObjId) {if ($ this-> Mode = 0) return; $ x = 'database error message :'. $ this-> _ Dbs_Error ($ ObjId); if ($ this-> Mode = 2) $ x. = "\ r \ nSQL = ". $ this-> LastSql; $ this-> _ SqlMsg ($ x); return false;} function _ SqlMsg ($ Txt, $ Color = '# ff0000 ') {if ($ this-> Mode! = 0) {echo'[TbsSql] '. nl2br (htmlentities ($ Txt )).' '. "\ R \ n"; flush () ;}} function _ SqlDate ($ Date, $ Mode) {// Return the date formated for the current Database if (is_string ($ Date) {$ x = strtotime ($ Date); if ($ x =-1) or ($ x = false) {// display error message $ this-> _ SqlMsg ('date value not recognized :'. $ Date); $ Mode = 0; // Try with the string mode $ x = $ Date;} else {$ x = $ Date ;} return $ this-> _ Dbs_Date ($ x, $ Mode);} function _ SqlProtect (& $ SQL, & $ ArgLst, $ IdxStart) {// Replace all % I % and @ I @ figures by corresponding protected values $ IdxMax = count ($ ArgLst)-1; for ($ I = $ IdxStart; $ I <= $ IdxMax; $ I ++) {// Simple value $ n = $ I-$ IdxStart + 1; $ tag = '% '. $ n. '%'; if (strpos ($ SQL, $ tag )! = False) {$ x = $ this-> _ Dbs_ProtectStr (''. $ ArgLst [$ I]); $ SQL = str_replace ($ tag, $ x, $ SQL);} // String value $ tag = '@'. $ n. '@'; if (strpos ($ SQL, $ tag )! = False) {$ x = '\''. $ this-> _ Dbs_ProtectStr (''. $ ArgLst [$ I]). '\ '''; $ SQL = str_replace ($ tag, $ x, $ SQL);} // Date value $ tag = '#'. $ n. '#'; if (strpos ($ SQL, $ tag )! = False) {$ x = $ this-> _ SqlDate ($ ArgLst [$ I], 1); $ SQL = str_replace ($ tag, $ x, $ SQL);} // Date and time value $ tag = '~ '. $ N .'~ '; If (strpos ($ SQL, $ tag )! = False) {$ x = $ this-> _ SqlDate ($ ArgLst [$ I], 2); $ SQL = str_replace ($ tag, $ x, $ SQL) ;}} if ($ this-> Mode = 2) {$ this-> LastSql = $ SQL;} elseif ($ this-> Mode = 3) {$ this-> _ SqlMsg ('trace SQL :'. $ SQL, '#663399') ;}// ------------------------------- // specify the Database system // --------------------------------- // Database Engine: MySQL // Version 1.02 function _ Dbs_Prepare () {if (@ mysql_ping () {// Check if a MySQL co Nnection already exist $ this-> Id = true;} function _ Dbs_Connect ($ srv, $ uid, $ pwd, $ db, $ drv) {$ Id = @ mysql_connect ($ srv, $ uid, $ pwd); if ($ Id! = False) and ($ db! = '') {If (! @ Mysql_select_db ($ db) $ this-> _ SqlError (false);} return $ Id;} function _ Dbs_Close () {if (is_resource ($ this-> Id )) {return @ mysql_close ($ this-> Id);} else {return @ mysql_close () ;}} function _ Dbs_Error ($ ObjId) {if (is_resource ($ this-> Id) {return @ mysql_error ($ ObjId);} else {return @ mysql_error () ;}} function _ Dbs_RsOpen ($ Src, $ SQL) {// $ Src is only for compatibility with TinyButStrong if (Is_resource ($ this-> Id) {return @ mysql_query ($ SQL, $ this-> Id);} else {return @ mysql_query ($ SQL );}} function _ Dbs_RsFetch (& $ RsId) {return mysql_fetch_assoc ($ RsId);} function _ Dbs_RsClose (& $ RsId) {if ($ RsId = true) return true; return @ mysql_free_result ($ RsId);} function _ Dbs_ProtectStr ($ Txt) {return mysql_real_escape_string ($ Txt);} function _ Dbs_Date ($ Timestamp, $ Mode) {switch ($ Mode ){ Case 1: // Date only return '\''. date ('Y-m-D', $ Timestamp ). '\ '''; case 2: // Date and time return '\''. date ('Y-m-d H: I: S', $ Timestamp ). '\ ''; case 0: // Value is a string return '\''. $ this-> _ Dbs_ProtectStr ($ Timestamp ). '\ ''; default: // Error in date recognization return' \ '2017-00-00 \'';} function _ Dbs_LastRowId () {return $ this-> Value (false, 'Select LAST_INSERT_ID () ');} function _ Dbs_AffectedRows () {If (is_resource ($ this-> Id) {return mysql_affected_rows ($ this-> Id);} else {return mysql_affected_rows () ;}}}?> This is a mysql database class that I have seen in a small Editor. if you have the honor to read it here, congratulations. You will soon become a master of php and mysql database operations in the future. |