PHP 对MySQLI预处理的包装
mysql 类
<?php
class Mysql {
private static $instance;
private $link;
private $query;
private $stmt;
private $param;
// 初始化
private function __construct() {
$this->link = @new mysqli('localhost', 'root', 'chenshuo90909', 'temp');
if(mysqli_connect_errno()) {
echo "MySQL connect error!"; exit();
}
return $this->link;
}
// 单例模式
public static function instance() {
if(isset(self::$instance)){
return self::$instance;
} else {
self::$instance = new self();
return self::$instance;
}
}
// 预处理SQL
private function prepare($query) {
$this->query = $query;
$this->stmt = $this->link->prepare($this->query);
if($this->stmt) {
return $this->stmt;
} else {
echo "Stmt error!"; exit;
}
}
// 值绑定
private function bind_value($array) {
$data = array();
foreach ($array as $key => $value) {
$data[$key] = &$array[$key];
}
return $data;
}
// 执行
public function execute($query, $param) {
$this->query = $query;
$this->stmt = $this->link->prepare($this->query);
$this->param = $param;
call_user_func_array(array($this->stmt, 'bind_param'), $this->bind_value($this->param)); //绑定参数
$result = $this->stmt->execute();
var_dump($result);
}
// 返回单挑数据
public function find($query, $param) {
$this->query = $query;
$this->param = $param;
$this->stmt = $this->link->prepare($this->query);
//绑定参数
call_user_func_array(array($this->stmt, 'bind_param'), $this->bind_value($this->param));
$this->stmt->execute();
$meta = $this->stmt->result_metadata();
// 将结果绑定数组元素设置为引用状态
while ($field = $meta->fetch_field()) {
$parameters[] = &$row[$field->name];
}
//绑定结果
call_user_func_array(array($this->stmt, 'bind_result'), $this->bind_value($parameters));
while ($this->stmt->fetch()) {
$result = $row;
}
return $result;
}
// 返回多条数据
public function fetch($query, $param) {
$this->query = $query;
$this->param = $param;
$this->stmt = $this->link->prepare($this->query);
//绑定参数
call_user_func_array(array($this->stmt, 'bind_param'), $this->bind_value($this->param));
$this->stmt->execute();
$meta = $this->stmt->result_metadata();
// 将结果绑定数组元素设置为引用状态
while ($field = $meta->fetch_field()) {
$parameters[] = &$row[$field->name];
}
//绑定结果
call_user_func_array(array($this->stmt, 'bind_result'), $this->bind_value($parameters));
// 有多行记录时将多行记录存入$results数组中.
while ($this->stmt->fetch()) {
$data = array();
foreach ($row as $key => $value) {
$data[$key] = $value;
}
$result[] = $data;
}
return $result;
}
// SQL语句调试
public function debug() {
return $this->query;
}
// 释放资源
public function __destruct() {
$this->stmt->close();
$this->link->close();
}
}
?>
应用:
<?php
include 'mysql.php';
class Data { const INSERT = "INSERT INTO user (username, password)VALUES(?, ?)";
const UPDATE = "UPDATE user SET username = ? WHERE uid = ?";
const SELECT = "SELECT username, password FROM user WHERE username = ? AND password = ?";
const DELETE = "DELETE FROM user WHERE uid = ?"; private $db; public function __construct() {
$this->db = Mysql::instance();
} public function insert($username, $password) {
$query = self::INSERT;
$param = array('ss',$username, $password);
$result = $this->db->execute($query, $param);
return $result;
} public function delete($uid) {
$query = self::DELETE;
$param = array('i',$uid);
$result = $this->db->execute($query, $param);
return $result;
} public function update($username, $uid) {
$query = self::UPDATE;
$param = array('si',$username, $uid);
$result = $this->db->execute($query, $param);
return $result;
} public function select($username, $password) {
$query = self::SELECT;
$param = array('ss',$username, $password);
$result = $this->db->find($query, $param);
return $result;
} public function find($username, $password) {
$query = self::SELECT;
$param = array('ss',$username, $password);
$result = $this->db->find($query, $param);
return $result;
} } $data = new Data(); //$data->insert('chenshuox', 'chenshuo123');
//$data->delete(2);
//$data->update('tinys123', 1); $result = $data->find('chenshuox', 'chenshuo123');
echo $result['username'];
echo $result['password']; ?>
PHP 对MySQLI预处理的包装的更多相关文章
- mysqli预处理和事务处理
1 应用环境 mysqli预处理功能(大量数据处理时使用) 2 步骤 a)mysqli连接数据库 $mysqli = new mysqli('localhost','root','root','chu ...
- php+mysqli预处理技术实现添加、修改及删除多条数据的方法
本文实例讲述了php+mysqli预处理技术实现添加.修改及删除多条数据的方法.分享给大家供大家参考.具体分析如下: 首先来说说为什么要有预处理(预编译)技术?举个例子:假设要向数据库添加100个用户 ...
- mysqli 预处理语句
预处理语句用于执行多个相同的 SQL 语句,并且执行效率更高. <?php // 设置编码格式 header('content-type:text/html;charset=utf-8'); / ...
- php mysqli 预处理操作数据库
用到的SQL表 CREATE TABLE `student_01` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARAC ...
- php mysqli扩展之预处理
在前一篇 mysqli基础知识中谈到mysqli的安装及基础操作(主要是单条sql语句的查询操作),今天介绍的是mysqli中很重要的一个部分:预处理. 在mysqli操作中常常涉及到它的三个主要类: ...
- PHP(Mysqli和PDO)预处理
PHP预处理主要是用来防SQL注入的,开发程序的都明白这样一个道理,不能相信用户的任何输入,如果用户输入问题你没有做相应的安全, 那么:你的程序是很危险的,很容易被攻击的!预处理:只分析两个:mysq ...
- PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)
原文: PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD) PHP 5 及以上版本建议使用以下方式连接 MySQL ...
- mysqli 操作数据库(转)
从php5.0开始增加mysql(i)支持 , 新加的功能都以对象的形式添加 i表示改进的意思 功能多.效率高.稳定 编译时参数: ./configure --with-mysql=/usr/bin/ ...
- mysqli 操作数据库
从php5.0开始增加mysql(i)支持 , 新加的功能都以对象的形式添加 i表示改进的意思 功能多.效率高.稳定 编译时参数: ./configure --with-mysql=/usr/bin/ ...
随机推荐
- Linux 添加ssh 公钥访问
登陆被管理的服务器,进入需要远程登陆的用户目录,把公钥放到用户目录的 .ssh 这个目录下(如果目录不存在,需要创建~/.ssh目录,并把目录权限设置为700),把公钥改名为authorized_ke ...
- vim 操作(转)
高效率移动编辑1.在插入模式之外基本上来说,你应该尽可能少的呆在插入模式里面,因为在插入模式里面 VIM 就像一个“哑巴”编辑器一样.很多新手都会一直呆在插入模式里面,因为这样易于使用.但 VIM 的 ...
- 直接拿来用!Facebook移动开源项目大合集
直接拿来用!Facebook移动开源项目大合集 时间:2014-04-22 15:37 作者:唐小引 随着iOS依赖管理工具CocoaPods和大量第三方开源库成熟起来,业界积累了大量的优秀开源项目. ...
- eclipse修改豆沙绿
长时间的使用eclipse开发会很累吧 设置一个保护眼睛的豆沙绿色 不刺眼 是不是会更好一些呢 那么如何设置呢现在就教大家 工具/原料 eclipse jdk 方法/步骤 1 首先打开eclip ...
- python顶级执行代码
只有主程序中由大量顶级执行代码(即没有被缩进的代码行),所有其他被导入的模块只应该又很少的顶级执行代码. 如果模块是被导入,__name__就是模块名. 如果模块是被直接执行,__name__就是__ ...
- java 持有对象
1.泛型和类型安全的容器 ArrayList,可以自动扩充大小的数组,add插入对象,get访问对象,size查看对象数目. 1 /** 2 * 泛型和类型安全的容器 3 * 2016/5/6 4 * ...
- final关键字修饰的变量
final意义:最终的,不可改变的. 1.修饰变量,为常量,值不可变: 2.修饰对象,值可变,引用不变: 3.修饰方法,方法不可重写: 4.修饰类,无子类,不可以被继承,更不可能被重写. 1.fina ...
- acm课程练习2--1013(同1014)
题目描述 There is a strange lift.The lift can stop can at every floor as you want, and there is a number ...
- hdu-1978_How many ways dfs+记忆化搜索
How many ways Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- mysql 查询 45 道题
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...