100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > php中关于MySQLI函数封装的数据库连接工具类的使用详解

php中关于MySQLI函数封装的数据库连接工具类的使用详解

时间:2022-11-16 01:52:35

相关推荐

php中关于MySQLI函数封装的数据库连接工具类的使用详解

后端开发|php教程

MySQLI,数据库,php

后端开发-php教程mysql.class.php:

怎么制作微信电影网站源码,vscode编写移动端网页,ubuntu运行网游,tomcat设置登录数量,app爬虫博客,mail php 发件人,四会seo优化方案,什么网站上看ASP.,宽屏php模板lzw

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; }}

使用方法

android ui布局 源码,vscode c 脚本,ubuntu rpi,lnmp加上tomcat,sqlite数据库制作,易语言写ie插件,前端框架简笔画儿童教程,大数据 爬虫 课程设计,php 是否post,seo类型的网站,商城购物网站源码,网上书店网页模板,安卓 模板 qlzw

mysql, host => localhost, username => woider, password => 3243, database => php, port => 3306);/* 连接数据库 */$mysql = new mysql();$mysql->connect($config);/* 查询数据 *///1、查询所有数据$table = mysqli;//数据表$num = $mysql->select($table);echo 共查询到 . $num . 条数据;print_r($mysql->fetchAll());//2、查询部分数据$field = array(username, password); //过滤字段$where = id % 2 =0;//过滤条件$mysql->select($table, $field, $where);print_r($mysql->fetchAll());/* 插入数据 */$table = mysqli;//数据表$data = array( //数据数组 username => admin, password => sha1(admin));$id = $mysql->insert($table, $data);echo 插入记录的ID为 . $id;/* 修改数据 */$table = mysqli;//数据表$data = array( password => sha1( imda));$where = id = 44;$rows = $mysql->update($table, $data, $where);echo 受影响的记录数量为 . $rows . 条;/* 删除数据 */$table = mysqli;$where = id = 45;$rows = $mysql->delete($table, $where);echo 已删除 . $rows . 条数据;

新闻app 源码,vscode运行键不见了,ubuntu 系统报错,tomcat设置虚拟端口,pyth爬虫下拉,php聊天室框架,河东区seo网络优化公司,淘宝客 没有网站lzw

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。