PHP基于MySQLI函数封装的数据库连接工具类【定义与用法】

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP基于MySQLI函数封装的数据库连接工具类【定义与用法】脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP基于MysqLI函数封装的数据库连接工具类。分享给大家供大家参考,具体如下:

MysqL.class.PHP

<PRe class="brush:PHp;"> MysqLi = new MysqLi($host,$username,$password,$database,$port); } /** * 数据查询 * @param $table 数据表 * @param null $field 字段 * @param null $where 条件 * @return mixed 查询结果数目 */ public function select($table,$field = null,$where = null) { $sql = "SELECT * From {$table}"; if (!empty($field)) { $field = '`' . implode('`,`',$field) . '`'; $sql = str_replace('*',$field,$sql); } if (!empty($where)) { $sql = $sql . ' WHERE ' . $where; } $this->result = $this->MysqLi->query($sql); return $this->result->num_rows; } /** * @return mixed 获取全部结果 */ public function fetchAll() { return $this->result->fetch_all(MysqLI_ASSOC); } /** * 插入数据 * @param $table 数据表 * @param $data 数据数组 * @return mixed 插入ID */ public function insert($table,$data) { foreach ($data as $key => $value) { $data[$key] = $this->MysqLi->real_escaPE_string($value); } $keys = '`' . implode('`,array_keys($data)) . '`'; $values = '\'' . implode("','",array_values($data)) . '\''; $sql = "INSERT INTO {$table}( {$keys} )VALUES( {$values} )"; $this->MysqLi->query($sql); return $this->MysqLi->insert_id; } /** * 更新数据 * @param $table 数据表 * @param $data 数据数组 * @param $where 过滤条件 * @return mixed 受影响记录 */ public function update($table,$data,$where) { foreach ($data as $key => $value) { $data[$key] = $this->MysqLi->real_escape_string($value); } $sets = array(); foreach ($data as $key => $value) { $kstr = '`' . $key . '`'; $vstr = '\'' . $value . '\''; array_push($sets,$kstr . '=' . $vstr); } $kav = implode(',',$sets); $sql = "UPDATE {$table} SET {$kav} WHERE {$where}"; $this->MysqLi->query($sql); return $this->MysqLi->affected_rows; } /** * 删除数据 * @param $table 数据表 * @param $where 过滤条件 * @return mixed 受影响记录 */ public function delete($table,$where) { $sql = "DELETE From {$table} WHERE {$where}"; $this->MysqLi->query($sql); return $this->MysqLi->affected_rows; } }

脚本宝典总结

以上是脚本宝典为你收集整理的PHP基于MySQLI函数封装的数据库连接工具类【定义与用法】全部内容,希望文章能够帮你解决PHP基于MySQLI函数封装的数据库连接工具类【定义与用法】所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。