PHP 数据库连接工具类(MySQLI函数包装)
====================mysql=====================
- <?php
- class mysql
- {
- private $mysqli;
- private $result;
- /**
- * 数据库连接
- * @param $config 配置数组
- */
- public function connect($config)
- {
- $host = $config['host']; //主机地址
- $username = $config['username'];//用户名
- $password = $config['password'];//密码
- $database = $config['database'];//数据库
- $port = $config['port']; //端口号
- $this->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;
- }
- }
mysql.class.php
====================使用方法=====================
- <?php
- require_once 'mysql.class.php';
- /* 配置连接参数 */
- $config = array(
- 'type' => '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('nimda')
- );
- $where = 'id = 44';
- $rows = $mysql->update($table, $data, $where);
- echo '受影响的记录数量为' . $rows . '条';
- /* 删除数据 */
- $table = 'mysqli';
- $where = 'id = 45';
- $rows = $mysql->delete($table, $where);
- echo '已删除' . $rows . '条数据';
PHP 数据库连接工具类(MySQLI函数包装)的更多相关文章
- 数据库连接工具类——包含取得连接和关闭资源 ConnUtil.java
package com.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepare ...
- 数据库连接工具类 数据库连接工具类——仅仅获得连接对象 ConnDB.java
package com.util; import java.sql.Connection; import java.sql.DriverManager; /** * 数据库连接工具类——仅仅获得连接对 ...
- JDBC连接数据库,结合DbUtil数据库连接工具类的使用
(以Mysql数据库为例) 第一步:在项目里配置数据库驱动 Build Path->configure Build Path ->Add Exteral JARs 将JDBC驱动包导 ...
- mysql数据库连接工具类C3P0
package com.dl.network_flow.db; import java.sql.Connection; import java.sql.PreparedStatement; impor ...
- mongo数据库连接工具类(C#)
Framework版本:.Net Framework 4 using System; using System.Collections.Generic; using System.Linq; usin ...
- MySQL数据库操作类(PHP实现,支持连贯操作)
<?php /** * Author: suvan * CreateTime: 2018/2/27 * description: 数据库操作类(仅对接MySQL数据库,主要利用MySQLi函数) ...
- 谈一下关于C++函数包装问题
在C++中,我们经常遇到在某个特定的时刻,需要将函数进行包装调用,尤其是当我们需要将不同签名的函数放到同一个集合时,由于函数签名不一致导致我们不能直接将各式各样的函数指针放到诸如list这样的集合中, ...
- 工具类之数据库工具类:DBUtil(採用反射机制)
常常操作数据库的码农们一定知道操作数据库是一项非常复杂的工作.它不仅要解决各种乱码的问题还要解决各种数据表的增删改查等的操作. 另外每次操作数据库都要用到数据库连接.运行SQL语句.关闭连接的操作.所 ...
- 通过使用集合Properties完成JDBC的连接工具类
1.将数据库连接对象所需参数保存在本地文件中 database.properties driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localh ...
随机推荐
- [LeetCode] First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- asp.net webservice返回json问题
使用jQuery $.ajax方法请求webservice 一.方法返回值为string,将json格式的字符串返回 设置contentType为"application/json;char ...
- Newtonsoft.Json 序列化和反序列化 时间格式【转】
1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg: A a=new A(); a.Name="Elain ...
- Android疑难杂症收集
在渲染前获取 View 的宽高 5种手势工具类 软键盘用法总结 Android中Shape的使用 Android只能动态注册的广播Action Android 悬浮窗权限各机型各系统适配大全 录音权限 ...
- HTML5学习总结-番外04 Cordova/PhoneGap
一 PhoneGap 1 PhoneGap简绍 http://www.cnblogs.com/JustRun1983/p/3819433.html 2 环境安装 http://cordova.apac ...
- JSPatch 技术要点
作者:干掉crash链接:https://zhuanlan.zhihu.com/p/21884786来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 如果有方法不遵守语言 ...
- HTTP Cookie详解
1.什么是HTTP Cookie? Wikipedia给出的定义是:An HTTP cookie is a small piece of data sent from a website and st ...
- 命名空间jquery
命名空间的意思就是 同一个元素有绑定了2个相同的事件,比如2个scroll,这个时候你做其他交互的时候只想触发第二个scroll事件 就可以用命名空间做了 <button id="b ...
- python 静态方法、类方法(二)
<Python静态方法.类方法>一文中曾用在类之外生成函数的方式,来计算类的实例的个数.本文将探讨用静态方法和类方法来实现此功能. 一使用静态方法统计实例 例1.static.py # - ...
- C3P0连接池配置和实现详解
一.配置 <c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> ...