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 ;}}}