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‘];

?>
时间: 2024-07-30 13:21:09

PHP 对MySQLI预处理的包装的相关文章

mysqli预处理和事务处理

1 应用环境 mysqli预处理功能(大量数据处理时使用) 2 步骤 a)mysqli连接数据库 $mysqli = new mysqli('localhost','root','root','chuanzhi'); b)设置编码 $mysqli->set_charset('gb2312'); c)发送query语句 使用$mysqli->prepare($query)这个方法表示是预处理,如果涉及到取值,必须指定查询字段 $query = "select id,proName,pr

php+mysqli预处理技术实现添加、修改及删除多条数据的方法

本文实例讲述了php+mysqli预处理技术实现添加.修改及删除多条数据的方法.分享给大家供大家参考.具体分析如下: 首先来说说为什么要有预处理(预编译)技术?举个例子:假设要向数据库添加100个用户,按常规思路,就是向数据库发送100个执行请求,此时,按照 mysql 数据库的工作原理,它需要对每一条执行语句进行编译(这里就有100次).所以,这里的效率是非常低的. 预处理(预编译)技术的作用,就是减少编译的次数和时间,以提高效果.通过一个案例来说明,预处理(预编译)技术是如何做到的(好吧,先

MYSQLI:mysqli预处理语句

应用环境 mysqli预处理功能(大量数据处理时使用) 步骤   a)mysqli连接数据库 $conn= new mysqli('localhost','root','123456','apple'); b)设置编码 $conn->set_charset('utf8'); c)发送query语句 使用$conn->prepare($sql)这个方法表示是预处理,如果涉及到取值,必须指定查询字段 $sql = "select * from product_info where pro

mysqli 预处理

应用环境 mysqli预处理功能(大量数据处理时使用) 步骤   a)mysqli连接数据库 $conn= new mysqli('localhost','root','123456','apple'); b)设置编码 $conn->set_charset('utf8'); c)发送query语句 使用$conn->prepare($sql)这个方法表示是预处理,如果涉及到取值,必须指定查询字段 $sql = "select * from product_info where pro

MySqli预处理

预处理是先提交SQL语句到服务端,执行预编译, 客户端执行SQL语句时,只需要上传输入参数即可. 如果涉及多次读取或存储,效率高于普通SQL执行操作. 1.普通SQL执行 <?php header('content-type:text/html;charset=utf-8'); $mysqli=new mysqli('localhost','root','','test'); $mysqli->query('set names utf8'); $sql="INSERT INTO us

MySQLi面向过程实践---预处理

MySQLi预处理涉及到以下几个函数: mysqli_stmt mysqli_prepare ( mysqli $link , string $query ) bool mysqli_stmt_bind_param ( mysqli_stmt $stmt , string $types , mixed &$var1 [, mixed &$... ] ) bool mysqli_stmt_execute ( mysqli_stmt $stmt ) bool mysqli_stmt_close

php mysqli扩展之预处理

在前一篇 mysqli基础知识中谈到mysqli的安装及基础操作(主要是单条sql语句的查询操作),今天介绍的是mysqli中很重要的一个部分:预处理. 在mysqli操作中常常涉及到它的三个主要类:MySQLi类,MySQL_STMT类,MySQLi_RESULT类.预处理主要是利用MySQL_STMT类完成的. 预处理是一种重要的 防止SQL注入的手段,对提高网站安全性有重要意义. 本文案例为 数据库名为test,数据表名为test,  字段有id ,title 两个,id自增长主键. 使用

mysqli 操作数据库(转)

从php5.0开始增加mysql(i)支持 , 新加的功能都以对象的形式添加 i表示改进的意思 功能多.效率高.稳定 编译时参数: ./configure --with-mysql=/usr/bin/mysql_config \ #使用 Mysql ClientLibrary(libmysql)构建 --with-mysqli=mysqlnd \ #使用 Mysql Native Dirver 即mysqlnd --with-pdo-mysql=mysqlnd #使用 Mysql Native

mysqli_stmt预处理类

预处理语句对于防止 MySQL 注入是非常有用的. 预处理语句及绑定参数 预处理语句用于执行多个相同的 SQL 语句,并且执行效率更高. 预处理语句的工作原理如下: 预处理:创建 SQL 语句模板并发送到数据库.预留的值使用参数 "?" 标记 .例如: INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?) 数据库解析,编译,对SQL语句模板执行查询优化,并存储结果不输出. 执行:最后,将应用绑定的值传递给参数