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/ ...
随机推荐
- Shell script fails: Syntax error: “(” unexpected
Shell script fails: Syntax error: “(” unexpected google 一下. http://unix.stackexchange.com/questions/ ...
- 全文检索工具推荐FileLocator
全文检索工具推荐FileLocator https://www.baidu.com/link?url=_vaDZaJ_OePrAX-BTUD5hjTymnvN7_1oIAnWyS25hqxAg0nUH ...
- C++ 学习之函数重载、基于const的重载
函数重载 函数重载的定义是:在相同的作用域中,如果函数具有相同名字而仅仅是形参表不同,此时成为函数重载.注意函数重载不能基于不同的返回值类型进行重载. 注意函数重载中的"形参表"不 ...
- zTree异步加载并初始化树时全部展开(贴主要代码)
<%@page pageEncoding="UTF-8"%> <%@include file="/commons/include/html_doctyp ...
- js中子页面父页面方法和变量相互调用
(1)子页面调用父页面的方法或者变量: window.parent.方法()或者变量名window.parent相当于定位到父页面 之后的操作和在父页面中写代码一样写 window.parent.aa ...
- 拖动滚动条时某一处相对另一处固定不动(position:fixed)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- CodeForces 567B Berland National Library hdu-5477 A Sweet Journey
这类题一个操作增加多少,一个操作减少多少,求最少刚开始为多少,在中途不会出现负值,模拟一遍,用一个数记下最大的即可 #include<cstdio> #include<cstring ...
- Java BigDecimal Class
Using BigDecimal to perform precise calculations with floats. BigDecimal is a class type. So declare ...
- Smoke Testing(冒烟测试)
Smoke Testing 的概念最早源于制造业,用于测试管道.测试时,用鼓风机往管道里灌烟,看管壁外面是否有烟冒出来,以便检验管道是否有缝隙.这一测试显然比较初级,更深层一点的测试至少要进行渗油测试 ...
- 10款免费Bootstrap后台模板演示及下载
自从有了类似Bootstrap这样强大的前端框架之后,无论我们是做静态页面,还是做网站主题,着实方便很多.即便有很多类似的其他国产.海外的前端框架比较,Bootstrap用户量以及功能文档还是比较大的 ...