I used PHP to encapsulate a mysql class in the DB database.

Source: Internet
Author: User
I used PHP to encapsulate a mysql class in the DB database.
 Conn = mysql_connect (DB_HOST, DB_USER, DB_PASS) or die ("connect failed ". mysql_error (); // select the database mysql_select_db (DB_DATABASENAME, $ this-> conn); // SET the encoding format mysql_query ("set names utf8 "); // var_dump ($ conn); $ this-> tablename = $ tablename;} // Set the SQL statement private function setsql ($ SQL) {$ this-> SQL = $ SQL;} // set the condition statement public function where ($ where) {$ this-> where = "where ". $ where; return $ this;} // Press the specified field public function field ($ keyword) {$ this-> fieldname = $ keyword; return $ this ;} // Set the connection query table public function table ($ table1, $ table2, $ field, $ bool) {$ this-> tablename = "$ table1 left join $ table2 ON $ table1. $ field $ bool $ table2. $ field"; // print_r ($ this-> tablename ); return $ this;} // set the public function addtable ($ table1, $ table2, $ field, $ bool) {$ this-> tablename. = "left join $ table2 ON $ table1. $ field $ bool $ table2. $ field"; // print_r ($ this-> tablename); return $ this ;} // Set the connection query table # SELECT * FROM [wx_order left join wx_shopcar ON orders = wx_order.oid and wx_order.uid = left join wx_goods ON orders = orders] WHERE wx_order.oid = 1 and wx_order.uid = 3 public function settable ($ SQL) {$ this-> tablename = $ SQL; // print_r ($ this-> tablename); return $ this ;}// query all databases and output the public function select () in an array () {/*** query all data in the database **/$ arr = array (); // execute the SQL statement $ result = mysql_query ("select ". $ this-> fieldname. "from ". $ this-> tablename. $ this-> where, $ this-> conn); while ($ row = mysql_fetch_assoc ($ result) {array_push ($ arr, $ row);} return $ arr ;} // search for the specified field data public function find () {// execute the SQL statement $ result = mysql_query ("select ". $ this-> fieldname. "from ". $ this-> tablename. $ this-> where, $ this-> conn); $ result = mysql_fetch_assoc ($ result); return $ result;} // add data to the database public function add ($ data) {$ keysql = ''; $ valuesql =''; foreach ($ data as $ key => $ value) {$ keysql. = ", '$ key"'; $ valuesql. = ", '$ value'" ;}$ keysql = substr ($ keysql, 1); $ valuesql = substr ($ valuesql, 1 ); $ result = mysql_query ("insert "'. $ this-> tablename. "'($ keysql) VALUES ($ valuesql)"); $ id = mysql_insert_id (); // print_r ("insert "'. $ this-> tablename. "'($ keysql) VALUES ($ valuesql)"); return $ id;} // modify the database content public function save ($ data) {$ keysql = ''; $ valuesql = ''; foreach ($ data as $ key => $ value) {$ keysql. = ", '$ key' =' $ value'";} $ keysql = substr ($ keysql, 1); // print_r ($ keysql); // echo"
"; $ Result = mysql_query (" UPDATE "'. $ this-> tablename. "'set ". $ keysql. $ this-> where); // print_r ("UPDATE "'. $ this-> tablename. "'set ". $ keysql. $ this-> where); return $ result ;}## delete data public function delete () {$ result = mysql_query ("delete from $ this-> tablename $ this-> where "); // print_r ("delete from $ this-> tablename $ this-> where"); return $ result ;}/ *** mysql_fetch_row: returns the fields in a single column [0] => "111" * mysql_fetch_field: obtains the field information. . [0] => ['name'] => object * mysql_fetch_array returns array data. [0] => "asasds" ['name'] => */?>


From: http://my.oschina.net/Peron/blog/552601

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.