正文  软件开发 > PHP编程技术 >

PHP封装的mysqli增、删、改、查的类,封装mysqli

PHP封装的mysqli增、删、改、查的类,封装mysqli  本文实例讲述了php封装的mysqli类。分享给大家供大家参考,不足之处欢迎指出。 1 <?php 2 3 clas...

PHP封装的mysqli增、删、改、查的类,封装mysqli

  本文实例讲述了php封装的mysqli类。分享给大家供大家参考,不足之处欢迎指出。

  1 <?php
  2 
  3 class Mysql
  4 {
  5 
  6     private $host;
  7 
  8     private $user;
  9 
 10     private $password;
 11 
 12     private $charset;
 13 
 14     private $database;
 15 
 16     /**
 17      * 新建数据库连接对象,测试数据库连接
 18      *
 19      * @param string $host            
 20      * @param string $user            
 21      * @param string $password            
 22      * @param string $charset            
 23      * @param string $database            
 24      */
 25     function __construct($host, $user, $password, $charset, $database)
 26     {
 27         $link = mysqli_connect($host, $user, $password) or die(‘数据库连接失败<br />ERROR ‘ . mysqli_connect_errno() . ‘:‘ . mysqli_connect_error());
 28         $char = mysqli_set_charset($link, $charset) or die(‘charset设置错误,请输入正确的字符集名称<br />ERROR ‘ . mysqli_errno($link) . ‘:‘ . mysqli_error($link));
 29         $db = mysqli_select_db($link, $database) or die(‘未找到数据库,请输入正确的数据库名称<br />ERROR ‘ . mysqli_errno($link) . ‘:‘ . mysqli_error($link));
 30         $this->host = $host;
 31         $this->user = $user;
 32         $this->password = $password;
 33         $this->charset = $charset;
 34         $this->database = $database;
 35         mysqli_close($link);
 36     }
 37 
 38     /**
 39      * 连接数据库
 40      *
 41      * @param string $host            
 42      * @param string $user            
 43      * @param string $password            
 44      * @param string $charset            
 45      * @param string $database            
 46      * @return object 连接标识符
 47      */
 48     private function connect($host, $user, $password, $charset, $database)
 49     {
 50         $link = mysqli_connect($this->host, $this->user, $this->password);
 51         mysqli_set_charset($link, $this->charset);
 52         mysqli_select_db($link, $this->database);
 53         return $link;
 54     }
 55 
 56     /**
 57      * 增加数据
 58      *
 59      * @param array $data            
 60      * @param string $table            
 61      * @return boolean
 62      */
 63     public function insert($data, $table)
 64     {
 65         $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database);
 66         $keys = join(‘,‘, array_keys($data));
 67         $vals = "‘" . join("‘,‘", array_values($data)) . "‘";
 68         $query = "INSERT INTO {$table}({$keys}) VALUES({$vals})";
 69         $result = mysqli_query($link, $query) or die(‘插入数据出错,请检查!<br />ERROR ‘ . mysqli_errno($link) . ‘:‘ . mysqli_error($link));
 70         if ($result) {
 71             $id = mysqli_insert_id($link);
 72         } else {
 73             $id = false;
 74         }
 75         mysqli_close($link);
 76         return $id;
 77     }
 78 
 79     /**
 80      * 删除数据
 81      *
 82      * @param string $table            
 83      * @param string $where            
 84      * @return boolean
 85      */
 86     public function delete($table, $where = null)
 87     {
 88         $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database);
 89         $where = $where ? ‘ WHERE ‘ . $where : ‘‘;
 90         $query = "DELETE FROM {$table}{$where}";
 91         $result = mysqli_query($link, $query) or die(‘删除数据出错,请检查!<br />ERROR ‘ . mysqli_errno($link) . ‘:‘ . mysqli_error($link));
 92         if ($result) {
 93             $row = mysqli_affected_rows($link);
 94         } else {
 95             $row = false;
 96         }
 97         mysqli_close($link);
 98         return $row;
 99     }
100 
101     /**
102      * 修改数据
103      *
104      * @param array $data            
105      * @param string $table            
106      * @param string $where            
107      * @return boolean
108      */
109     public function update($data, $table, $where = null)
110     {
111         $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database);
112         $set = ‘‘;
113         foreach ($data as $key => $val) {
114             $set .= "{$key}=‘{$val}‘,";
115         }
116         $set = trim($set, ‘,‘);
117         $where = $where ? ‘ WHERE ‘ . $where : ‘‘;
118         $query = "UPDATE {$table} SET {$set}{$where}";
119         $result = mysqli_query($link, $query) or die(‘数据修改错误,请检查!<br />ERROR ‘ . mysqli_errno($link) . ‘:‘ . mysqli_error($link));
120         if ($result) {
121             $row = mysqli_affected_rows($link);
122         } else {
123             $row = false;
124         }
125         mysqli_close($link);
126         return $row;
127     }
128 
129     /**
130      * 查询指定记录
131      *
132      * @param string $query            
133      * @param string $result_type            
134      * @return array|boolean
135      */
136     public function select_one($query, $result_type = MYSQLI_ASSOC)
137     {
138         $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database);
139         $result = mysqli_query($link, $query) or die(‘查询语句错误,请检查!<br />ERROR ‘ . mysqli_errno($link) . ‘:‘ . mysqli_error($link));
140         if ($result && mysqli_num_rows($result) > 0) {
141             $row = mysqli_fetch_array($result, $result_type);
142         } else {
143             $row = false;
144         }
145         mysqli_free_result($result);
146         mysqli_close($link);
147         return $row;
148     }
149 
150     /**
151      * 查询所有记录
152      *
153      * @param string $query            
154      * @param string $result_type            
155      * @return array|boolean
156      */
157     public function select_all($query, $result_type = MYSQLI_ASSOC)
158     {
159         $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database);
160         // $query = "SELECT * FROM {$table}";
161         $result = mysqli_query($link, $query) or die(‘查询语句错误,请检查!<br />ERROR ‘ . mysqli_errno($link) . ‘:‘ . mysqli_error($link));
162         if ($result && mysqli_num_rows($result) > 0) {
163             while ($row = mysqli_fetch_array($result, $result_type)) {
164                 $rows[] = $row;
165             }
166         } else {
167             $rows = false;
168         }
169         mysqli_free_result($result);
170         mysqli_close($link);
171         return $rows;
172     }
173 
174     /**
175      * 得到表中记录数
176      *
177      * @param string $table            
178      * @return number|boolean
179      */
180     public function get_total_rows($table)
181     {
182         $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database);
183         $query = "SELECT COUNT(*) AS totalRows FROM {$table}";
184         $result = mysqli_query($link, $query);
185         if ($result && mysqli_num_rows($result) == 1) {
186             $row = mysqli_fetch_assoc($result);
187         } else {
188             $row[‘totalRows‘] = false;
189         }
190         mysqli_close($link);
191         return $row[‘totalRows‘];
192     }
193 }

http://www.bkjia.com/PHPjc/1231552.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1231552.htmlTechArticlePHP封装的mysqli增、删、改、查的类,封装mysqli 本文实例讲述了 php 封装的 mysqli 类。分享给大家供大家参考,不足之处欢迎指出。 1 ? php 2...