Encapsulate MySQL class-PHP source code by using chained call

Source: Internet
Author: User
Tags mysql code
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 ();?>

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.