Encapsulate MySQL code by using chained call
Host = $ server; $ this-> name = $ dbuser; $ this-> password = $ psw; $ this-> link = @ mysql_connect ($ this-> host, $ this-> name, $ this-> password) or die ('Error:Database host connection failed!
'); $ This-> runStat = "init"; // defined as initialization $ this-> fn = 0;} public function to2DArray () {// Construct a two-dimensional array // detection // if (! Is_resource ($ this-> value) {array_push ($ this-> errlog, "ERROR:Running error, inTo2DArrayMethod of layer call error
"); $ This-> runStat =" error "; // Set the running status to error return $ this;} $ this-> runStat =" to2DArray "; /// ///$ _ 2 DArray = Array (); while ($ row = @ mysql_fetch_array ($ this-> value) {// directly obtain the value. at this time, the value should be of the resource type $ keyarr = array_keys ($ row ); foreach ($ keyarr as $ key) {if (is_int ($ key) {unset ($ row [$ key]); // remove the array element with the array key name};} array_push ($ _ 2 DArray, $ row);} $ this-> value = $ _ 2 DArray; return $ this;} public function Db ($ database, $ charset) {// connect to the database // Check // if (! Is_resource ($ this-> link) {array_push ($ this-> errlog, "ERROR:Running error, inDbMethod of layer call error
"); $ This-> runStat =" error "; // Set the running status to error return $ this;} $ this-> runStat =" db "; /// ///$ this-> dbname = $ database; mysql_query ("set names ". $ charset); // set the character set $ this-> value = mysql_select_db ($ this-> dbname, $ this-> link); if (! $ This-> value) {echo"ERROR: An ERROR occurred while opening the database!
"; $ This-> runStat =" error ";} return $ this;} public function query ($ SQL) {// detection // if (! Is_string ($ SQL) {array_push ($ this-> errlog, "ERROR:Running error, inQueryMethod of layer call error
"); $ This-> runStat =" error "; // Set the running status to error return $ this;} $ this-> runStat =" query "; /// // $ SQL = addslashes ($ SQL ); // Anti-injection $ result = mysql_query ($ SQL); if (! $ Result) {array_push ($ this-> errlog, "ERROR:Statement running error!
"); $ This-> runStat =" sqlerror ";} $ this-> value = $ result; return $ this;} function value () {$ this-> runStat = "init"; if ($ this-> errreport) {foreach ($ this-> errlog as $ value) {echo $ value ;}} $ this-> errlog = array (); // clean $ result = $ this-> value; $ this-> value = null; // cleanreturn $ result ;} public function _ call ($ f, $ v) {// The error method absorbs echo"ERROR: The ". $ f." () method does not exist.
"; Return $ this ;}$ db = new MySQL (" localhost "," root "," lijun "); // $ db-> errreport = false; $ h = $ db-> db ("students", 'utf8'); var_dump ($ db-> db ("ip", 'utf8')-> to2DArray () -> query ("select * from ip limit 0, 2")-> to2DArray ()-> value ()); var_dump ($ h-> query ("seslect * from nuser limit 10, 2")-> to2DArray ()-> value ()); var_dump ($ db-> db ("ip", 'utf8')-> query ("SELECT * FROM ip LIMIT 0, 2")-> to2DArray () -> value ();?>