PHP MySQL database class (compatible with PHP4 and PHP5)

Source: Internet
Author: User
Tags php and mysql php mysql
This article introduces the PHP implementation of the MySQL database operation class, this class is characterized by compatibility with PHP4 and PHP5, it is worth learning. Please refer to the friends who are in need.

Share a PHP and MySQL database class, good compatibility 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-&G T         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 Lastrowid () {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 (Htmlen Tities ($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 (' Dat                 E 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 Correspondin         G 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 Database System//-------------------------------//Database Engine:mysql//Vers Ion 1.02 FUnction _dbs_prepare () {if (@mysql_ping ()) {//Check if a MySQL connection already exist $this->i         D = 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-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 ' \ ' 0000-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 small part of a MySQL database class that is quite a bull x, if you have the pleasure to read it, then congratulations. In the future of PHP and MySQL database operations, you will become a master.

  • Related Article

    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.