Php object-oriented design Mysql database operations

Source: Internet
Author: User
Php object-oriented design Mysql database operations
 Host =! Empty ($ config ['host'])? $ Config ['host']: "localhost"; $ this-> port =! Empty ($ config ['port'])? $ Config ['port']: "3306"; $ this-> user =! Empty ($ config ['user'])? $ Config ['user']: "root"; $ this-> password =! Empty ($ config ['password'])? $ Config ['password']: ""; $ this-> charset =! Empty ($ config ['charset'])? $ Config ['charset']: "utf8"; $ this-> dbname =! Empty ($ config ['dbname'])? $ Config ['dbname']: ""; $ this-> link = mysql_connect ("{$ this-> host }:{$ this-> port }", "{$ this-> user}", "{$ this-> password}") or die ('execution failed '); $ this-> setCharset ($ this-> charset); $ this-> useDBname ($ this-> dbname);} // Singleton design mode static $ temp = null; static function getClass () {if (! Isset (self ::$ temp) {$ class = new self () ;}else {return self ::$ temp ;}} // you can set the encoding function setCharset ($ charset) {mysql_query ("set names $ charset");} // you can set the function useDBname ($ dbname) of the database to be connected) {mysql_query ("use $ dbname") ;}// you can disable the database function closeDB () {mysql_close ($ this-> link); echo "close successful! ";}// Execute an add, delete, and modify statement, and return the true and false results: function exec ($ SQL) {$ result = $ this-> query ($ SQL); return true ;} // return the query data function gerRow ($ SQL) {$ result = $ this-> query ($ SQL); $ array = mysql_fetch_array ($ result ); mysql_free_result ($ result); // release resource return $ array;} // return multi-row query data function getRows ($ SQL) {$ result = $ this-> query ($ SQL); $ arr = array (); while ($ array = mysql_fetch_array ($ result )) {$ arr [] = $ array;} mysql_free_result ($ result); // release the resource return $ arr;} // return a Data statement, you can return a direct value function getOneData ($ SQL) {$ result = $ this-> query ($ SQL); $ array = mysql_fetch_array ($ result ); $ data = $ array [0]; mysql_free_result ($ result); // release the resource return $ data;} // you can execute any SQL statement and handle errors, or return the execution result function query ($ SQL) {$ result = mysql_query ($ SQL, $ this-> link); if ($ result = false) {echo "execution failed. See the following information:"; echo"
Error code: ". mysql_errno (); echo"
Error message: ". mysql_error (); echo"
Error statement: ". $ SQL; die () ;}else {return $ result ;}}}

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.