I used PHP to encapsulate a DB database MySQL class

Source: Internet
Author: User
 conn = mysql_connect (Db_host, Db_user, Db_pass) or Die ("Connect Failed". Mysql_error ()); Select Database mysql_select_db (db_databasename, $this->conn); Set encoding format mysql_query ("Set NAMES UTF8");//var_dump ($conn); $this->tablename= $tablename;} Set SQL statement Private Function SetSQL ($sql) {$this->sql= $sql;} Set the conditional 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 connection query table Public function table ($table 1, $table 2, $field, $bool) {$this->tablename= "$table 1 left JOIN $table 2 on $ Table1. $field $bool$table2 $field ";//print_r ($this->tablename); return $this;} Set multi-table query Public Function addtable ($table 1, $table 2, $field, $bool) {$this->tablename.= ' left JOIN $table 2 on $table 1.$ Field$bool$table2. $field ";//print_r ($this->tablename); return $this;} Set the connection query table # #SELECT * from "Wx_order left JOIN Wx_shopcar on wx_shopcar.oid=wx_order.oid and Wx_order.uid=wx_shopcar.uid LE FT JOIN wx_goods on Wx_shopcar.gid=wx_gOods.gid "WHERE wx_order.oid=1 and Wx_order.uid=3public function settable ($sql) {$this->tablename= $sql;//print_r ($ This->tablename); return $this;} Queries all databases to output public function select () {/*** queries the database for all data **/$arr =array ();//Execute 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 Specifies field data public function find () {//Execute 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 into". $this->tablename. " ($keysql) VALUES ($valuesql); $id =mysql_insert_id ();//print_r ("INSERT into". $this->tablename. " ` ($keysql) VALUES ($valuesql) "); return $id;} Modify the contents of the database 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;} # #删除数据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 of the single column [0]=> "111" * Mysql_fetch_field: Gets the field information. [0]=> [' name ']=> object* mysql_fetch_array returns the array data. [0]=> "Asasds" [' Name ']=>*/?>


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

  • Related Article

    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.