Database operation model based on PHP mysqli extension

Source: Internet
Author: User
 ' 192.168.1.246 ', ' username ' = ' liuchuan ', ' password ' = ' liuchuan123 ', ' database ' =        ' Whatcart ');        $this->link=new mysqli ($dbinfo [' hostname '], $dbinfo [' username '], $dbinfo [' Password '], $dbinfo [' database ']; if ($this->link->connect_errno) trigger_error (' Error:could not make a database link ('. $this->link-> Connect_errno. ') '.        $this->link->connect_errno);        $this->link->set_charset ("UTF8");        $this->fieldstring= ' * ';        $this->tablestring= $tableName;        $this->leftjoinstring= ';        $this->wherestring= ';        $this->orderstring= ';    $this->limitstring= '; }/******** encapsulated Mysqli extension method ********///parameter filtering, input parameters can only be basic type or one-dimensional array public function escape ($param) {if (Is_array ($PA RAM) {foreach ($param as $key + $value) {$param [$key]= $this->link->real_escape_strin            g ($value); }}else{$param =$this->link->real_escape_string ($value);    } return $param;    }//Gets the ID generated after insert Public function Getlastid () {return $this->link->insert_id;    }//Gets the number of rows affected public function countaffected () {return $this->link->affected_rows;        }//Execute SQL command returns TRUE or False Public function execute ($sql) {$this->sql= $sql;        $result = $this->link->real_query ($this->sql); if (false=== $result) {trigger_error ('
ERROR MESSAGE: '. $this->link->error. '
The SQL: '. $this->sql); return false; }else{$sql =strtolower ($this->sql); Return (False===strpos ($sql, ' Insert ')? True: $this->link->insert_id;//If an insert is included to return the ID of the inserted record}}//Execute SQL query return Back associative array public function query ($sql) {$result = $this->link->real_query ($sql); $this->sql= $sql; if ($result!==false) {$record =array (); $list =array (); while ($record = $result->fetch_assoc ()) {$list []= $record; } return $list; }else{Trigger_error ('
ERROR MESSAGE: '. $this->link->error. '
The SQL: '. $this->sql); return false; }}/******** single table additions and deletions ********/public function Create ($table, $model) {$fields = '; $values = "; foreach ($model as $key = + $value) {if ($fields) {$fields. = ', '; } $fields. = "' $key '"; if ($values) {$values. = ', '; $values. =is_numeric ($value)? $value: "'". $this->link->real_escape_string ($value). "'"; } $this->sql= "INSERT into ' $table ' ($fields) VALUES ($values)"; if (false=== $this->link->real_query ($this->sql)) Trigger_error ('
ERROR MESSAGE: '. $this->link->error. '
The SQL: '. $this->sql); return $this->link->insert_id; Public function Modify ($table, $model, $where) {$assins = '; $where = $this->rewhere ($where); foreach ($model as $key = = $value) {$rkey = $this->link->real_escape_string ($key); $rvalue = $this->link->real_escape_string ($value); if (!is_numeric ($rvalue)) {$rvalue = "'". $rvalue. "'"; } $assins. = $assins? ", ' $rkey ' = $rvalue": "' $rkey ' = $rvalue '; } $result = $this->link->real_query ($this->sql); if (false=== $result) trigger_error ('
ERROR MESSAGE: '. $this->link->error. '
The SQL: '. $this->sql); The Public function remove ($table, $where) {$where = $this->rewhere ($where); $this->sql= "DELETE from ' $table ' WHERE $where '; $result = $this->link->real_query ($this->sql); if (false=== $result) trigger_error ('
ERROR MESSAGE: '. $this->link->error. '
The SQL: '. $this->sql); return $flag; The Public function unique ($where) {$where = $this->rewhere ($where); $this->sql= "select * from ' $table ' WHERE $where LIMIT 1"; $result = $this->link->real_query ($this->sql); if ($result ===false) trigger_error ('
ERROR MESSAGE: '. $this->link->error. '
The SQL: '. $this->sql); Return $result, Fetch_object (); The Public Function Countrow ($where) {$where = $this->rewhere ($where); $this->sql= ($where = = ")? Select COUNT (*) as ' totalrow ' from ' {$this->tablestring} ' ":" SELECT COUNT (*) from ' {$this->tablestring} ' WHERE $ where "; $result = $this->link->real_query ($this->sql); if ($result ===false) trigger_error ('
ERROR MESSAGE: '. $this->link->error. '
The SQL: '. $this->sql); $record = $result->fetch_object (); return $record->totalrow; The function search ($table, $fields, $leftJoin = ', $where = ', er = ', $limit = ') {if (Is_array ($f ields)) $fields =implode (",", $fields); $this->sql=empty ($fields)? " SELECT * from ' $table ' ":" Select $fields from ' $table ' "; if (!empty ($where)) {$where = $this->rewhere ($where); $this->sql.= "WHERE $where"; } if (!empty (er)) $this->sql.= "ORDER by er"; if (!empty (er)) $this->sql.= "LIMIT $limit"; $result = $this->link->real_query ($this->sql); if ($result ===false) {trigger_error ('
ERROR MESSAGE: '. $this->link->error. '
The SQL: '. $this->sql); }else{$tempItem =array (); $tempList =array (); while ($tempItem = $result, Fetch_assoc ()) {if (!empty ($tempItem [' create_time '])) $temp item[' Create_time_exp ']=date (' y-m-d h:i:s ', $tempItem [' create_time ']); if (!empty ($tempItem [' add_time ')) $tempItem [' Add_time_exp ']=date (' y-m-d h:i:s ', $tempItem [' end_time ']); if (!empty ($tempItem [' start_time '])) $tempItem [' Start_time_exp ']=date (' y-m-d h:i:s ', $temp item[' start_time '); if (!empty ($tempItem [' end_time ')) $tempItem [' End_time_exp ']=date (' y-m-d h:i:s ', $tempItem [' end_time ']); $tempList []= $tempItem; }; return $tempList; }} Public Function Keyin ($ids) {if (!empty ($ids)) {return false; } if (Is_array ($ids)) {foreach ($ids as $key = = $value) {$ids[$key]= $this->link->real_escape_string ($value); } $ids =implode (', ', $ids); }else{$ids = $this->link->real_escape_string ($ids); } $primary = $this->getprimarykey (); if (!empty ($this->wherestring)) $this->wherestring.= "and"; $this->wherestring.= "' $primary ' in ($ids)"; return $this; }/****************** thinkphp chain Operation *******************************///Set the field of the Query Public function field ($field) {i F (Is_array ($field)) $field =implode (', ', $field); $this->fieldstring= $field; return $this; The Public Function table ($table) {$this->tablestring= $table; The Public Function order (er) {$this->orderstring= er; The Public function limit ($limit) {$this->limitstring= $limit; }//Assemble the conditions of the array format into a string public function where ($where) {if (Is_array ($where) &&count ($where) >0) { $str = "; foreach ($where as $key = + $value) {if (!empty ($STR)) {$str. = ' and '; } $rekey = $this->link->real_escape_string ($key); $revalue = $this->link->real_escape_string ($value); $str. =is_numeric ($revalue)? " ' $rekey ' = $revalue ":" ' $rekey ' = ' $revalue ' "; } $this->wherestring= $str; }else{$this->wherestring= $where; } return $this; }//Delete public Function Delete () {if (Empty ($this->wherestring)) Trigger_error ('
ERROR MESSAGE: Delete condition cannot be null '); $this->sql= "DELETE from {$this->tablestring} WHERE {$this->wherestring}"; }//Update public Function Update ($model) {if (Empty ($this->wherestring)) Trigger_error ('
ERROR MESSAGE: Update condition cannot be null '); if (Empty ($model) | | Count ($model) ==0) trigger_error ('
ERROR MESSAGE: Update parameter cannot be empty ') $assins = '; foreach ($model as $key = = $value) {$rkey = $this->link->real_escape_string ($key); $rvalue = $this->link->real_escape_string ($value); if (!is_numeric ($rvalue)) {$rvalue = "'". $rvalue. "'"; } $assins. = $assins? ", ' $rkey ' = $rvalue": "' $rkey ' = $rvalue '; } $this->sql= "UPDATE {$this->tablestring} SET $assins WHERE {$this->wherestring}"; }//Query public function Select () {$this->sql= "select {$this->fieldstring} from {$this->tablestring}"; if (!empty ($this->wherestring)) $this->sql.= ' WHERE '. $this->wherestring; if (!empty ($this->orderstring)) $this->sql.= ' ORDER by '. $this->orderstring; if (!empty ($this->limitstring)) $this->sql.= ' LIMIT '. $this->limitstring; return $this->query ($this-$sql); }/* The statement that the development environment uses for debugging, the production environment can delete */Public Function GetSQL () {return $this->sql; }}
  • 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.