PHP encapsulates a DB database mysql class
Release: 2016-07-23 08:54:58
Original
983 people have browsed it
- // Configure the database
- define('DB_HOST', '127.0.0.1'); // Server address
- define('DB_USER', 'root'); //Username
- define('DB_PASS', ''); //Password
- define('DB_DATABASENAME', 'fenxiao'); //Database
- class Dbmysql
- {
-
- /*
- *Variable
- **/
- private $tablename=""; //table name
- private $fieldname="*";
- private $conn;
- private $where;
- private $sql;
- function __construct($tablename)
- {
- //Generate a connection
- $this->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 ;
-
- }
-
-
-
- //Set conditional statements
-
- public function where($where)
-
- {
-
- $this->where=" where ".$where;
-
-
-
- return $this;
-
- }
-
-
-
- //By specified field
-
- public function field($keyword)
-
- {
-
- $this->fieldname=$keyword;
-
- return $this;
-
- }
-
- //Set up 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 up multi-table query
-
- 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 up 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 LEFT JOIN wx_goods ON wx_shopcar.gid=wx_goods.gid] 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 them in array form
-
- public function select()
-
- {
-
- /**
-
- * Query all data in the database
-
- * */
-
- $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 for specified 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 database The contents of
-
- 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 each field of a single column [0]=>"111"
-
- * mysql_fetch_field: Gets field information. [0]=> ['name']=> object
-
- * mysql_fetch_array returns array data. [0]=>"asasds" ['name']=>
-
- */
-
- ?>
Copy code
|
Installed, PHP, mysql
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31