php mysql 增删改查 封装_PHP封装的mysqli增、删、改、查的类

1 <?php2

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 $host20 * @param string $user21 * @param string $password22 * @param string $charset23 * @param string $database24 */

25 function __construct($host, $user, $password, $charset, $database)26 {27 $link = mysqli_connect($host, $user, $password) or die('数据库连接失败
ERROR ' . mysqli_connect_errno() . ':' . mysqli_connect_error());28 $char = mysqli_set_charset($link, $charset) or die('charset设置错误,请输入正确的字符集名称
ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link));29 $db = mysqli_select_db($link, $database) or die('未找到数据库,请输入正确的数据库名称
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 $host42 * @param string $user43 * @param string $password44 * @param string $charset45 * @param string $database46 * @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 $data60 * @param string $table61 * @return boolean62 */

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('插入数据出错,请检查!
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 $table83 * @param string $where84 * @return boolean85 */

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('删除数据出错,请检查!
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 $data105 * @param string $table106 * @param string $where107 * @return boolean108 */

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('数据修改错误,请检查!
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 $query133 * @param string $result_type134 * @return array|boolean135 */

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('查询语句错误,请检查!
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 $query154 * @param string $result_type155 * @return array|boolean156 */

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('查询语句错误,请检查!
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 $table178 * @return number|boolean179 */

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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值