<?phpheader ("Content-type:text/html;charset=utf-8");/** *php operation MySQL Tool class */class db{private $_db = null;// Database connection handle Private $_table = null;//table name Private $_where = null;//where condition Private $_order = Null;//order sort private $_limit = null;/ /limit Limited Query Private $_group = Null;//group Group Private $_configs = Array (' hostname ' = ' localhost ', ' dbname ' = ' test ', ' Username ' + ' root ', ' password ' = ' 1234 ');//Database Configuration/** * constructor, Connection database */public function __construct () {$link = $this-> ; _db;if (! $link) {$db = Mysqli_connect ($this->_configs[' hostname '), $this->_configs[' username '], $this->_ configs[' password '), $this->_configs[' dbname '); Mysqli_query ($db, "Set names UTF8"); if (! $db) {$this Showexception ("error message". Mysqli_connect_error ());} $this->_db = $db;}} /** * Get ALL data * * @param <type> $table the table * * @return Boolean all. */public function GetAll ($table =null) {$link = $this->_db;if (! $link) return false; $sql = "Select * FROM {$table}"; $data = Mysqli_fetch_all ($this->exeCute ($sql)); return $data;} Public function table ($table) {$this->_table = $table; return $this;} /** * Implement query Operation * * @param string $fields The fields * * @return Boolean (description_of_the_return_value) */p ublic function Select ($fields = "*") {$fieldsStr = ' '; $link = $this->_db;if (! $link) return false;if (Is_array ($fields)) {$fieldsStr = implode (', ', $fields);} ElseIf (is_string ($fields) &&!empty ($fields)) {$fieldsStr = $fields;} $sql = "Select {$fields} from {$this->_table} {$this->_where} {$this->_order} {$this->_limit}"; $data = Mysqli_fetch_all ($this->execute ($sql)); return $data;} /** * Order Sort * * @param string $order the order * * @return Boolean (description_of_the_return_value) */pu Blic function Order ($order = ') {$orderStr = '; $link = $this->_db;if (! $link) return false;if (Is_string ($order) & &!empty ($order)) {$orderStr = "order by". $order;} $this->_order = $orderStr; return $this;} /** * WHERE Condition * * @param string $where the where * * @return <type> (description_of_the_return_value) */public function where ($where = ') {$whereStr = ' ; $link = $this->_db;if (! $link) return $link, if (Is_array ($where)) {foreach ($where as $key + = $value) {if ($value = = = EN D ($where)) {$whereStr. = "'". $key. " ' = '. $value. "'";} else{$whereStr. = "'". $key. " ' = '. $value. "' and ";}} $WHERESTR = "WHERE". $whereStr;} ElseIf (is_string ($where) &&!empty ($where)) {$whereStr = "where". $where;} $this->_where = $whereStr; return $this;} /** * Group Group * * @param string $group the group * @return Boolean (description_of_the_return_value) */pu Blic function Group ($group = ') {$groupStr = '; $link = $this->_db;if (! $link) return false;if (Is_array ($group)) {$ Groupstr = "GROUP by". Implode (', ', $group);} ElseIf (is_string ($group) &&!empty ($group)) {$groupStr = "group by". $group;} $this->_group = $groupStr; return $this;} /** * Limit Limit Query * * @param string $limit the limit * * @return <type> (desCription_of_the_return_value) */public function limit ($limit = ") {$limitStr ="; $link = $this->_db;if (! $link) return $link; if (is_string ($limit) | |! Empty ($limit)) {$limitStr = "limit". $limit;} ElseIf (Is_numeric ($limit)) {$limitStr = "limit". $limit;} $this->_limit = $limitStr; return $this;} /** * Execute SQL statement * * @param <type> $sql the SQL * * @return Boolean (Description_of_the_return_value) */public function Execute ($sql =null) {$link = $this->_db;if (! $link) return false; $res = Mysqli_query ($this->_db,$ SQL), if (! $res) {$errors = Mysqli_error_list ($this->_db); $this->showexception ("Error! <br/> error Number: ". $errors [0][' errno ']." <br/>sql Error Status: ". $errors [0][' SQLState ']." <br/> error message: ". $errors [0][' Error ']);d ie ();} return $res;} /** * Insert Data * * @param <type> $data the data * * @return Boolean (description_of_the_return_value) */ Public Function Insert ($data) {$link = $this->_db;if (! $link) return false;if (Is_array ($data)) {$keys = "; $values ="; foreach ($data as $key = = $value) {$keys. = "'". $key. " ', '; $values. = "'". $value. "',";} $keys = RTrim ($keys, ', '); $values = RTrim ($values, ', ');} $sql = "INSERT into ' {$this->_table} ' ({$keys}) VALUES ({$values})"; Mysqli_query ($this->_db, $sql); $insertId mysqli_insert_id ($this->_db); return $insertId;} /** * Update Data * * @param <type> $data the data * * @return <type> (description_of_the_return_value ) */public function Update ($data) {$link = $this->_db;if (! $link) return $link; if (Is_array ($data)) {$dataStr = '; foreach ($data as $key = = $value) {$dataStr. = "'". $key. " ' = '. $value. "',";} $DATASTR = RTrim ($dataStr, ', ');} $sql = "UPDATE ' {$this->_table} ' SET {$dataStr} {$this->_where} {$this->_order} {$this->_limit}"; $res = $ This->execute ($sql); return $res;} /** * Delete Data * * @return <type> (description_of_the_return_value) */public function Delete () {$link = $this _db;if (! $link) return $link; $sql = "DELETE from ' {$this->_table} ' {$tHis->_where} "; $res = $this->execute ($sql); return $res;} /** * Exception Information Output * * @param <type> $var the variable */privatefunction showexception ($var) {if (Is_bool ($var)) {VA R_dump ($var);} else if (Is_null ($var)) {var_dump (null);} Else{echo "<pre style= ' position:relative;z-index:1000;padding:10px;border-radius:5px;background: #F5F5F5; border:1px solid #aaa; font-size:14px;line-height:18px;opacity:0.9; ' > ". Print_r ($var, true)." </pre> ";}}} $db = new db ();//query Operation Var_dump ($db->table (' user ')->where (' ID > 2 ')->order (' id desc ')->limit (' 2,4 ')- >select ());//insert Operation Var_dump ($db->table (' user ')->insert (Array (' username ' = ' user ', ' password ' = ' pwd ') ));//Update Operation Var_dump ($db->table (' user ')->where (' id = 1 ')->update (Array (' username ' = = ' user1 ', ' password ' = > ' pwd1 '));//delete operation Var_dump ($db->table (' user ')->where (' id = 1 ')->delete ());
Encapsulates a DB Class (simple version) similar to the thinkphp coherent operations database.