Mysqli的预编译机制处理批量数据过程

mysqli增强,还有一部分是对事物处理机制和预编译机制的增加,其实这两者都是为安全和执行效率着想的,这里主要说一下mysqli的预编译机制。

所谓的预编译,并不是在php的内核进行编译,而是数据库管理系统进行预编译,由于用于批量数据,说白了就是把一部分固定的数据格式先在mysql上面进行一次编译,编译之后就不在对其进行再次编译,我们要做的就是,向编译的占位符(就是数据占位)添加数据,之后发送,这样的话,大大的减少了编译次数,提高了批处理的效率。

下面是一段简单的示例代码:

<?php
	$mysqli=new MySQLi("localhost","root","toor","test");
	$sql="insert into account (id,blance) values (?,?)";//也可以多个问好,逗号隔开
	$mysqli_stmt=$mysqli->prepare("$sql");//创造预编译对象
	/**每一条数据发送都是独立执行的*/
	$id=6;
	$blance=23;
	$mysqli_stmt->bind_param("id",$id,$blance);//添加
	$b=$mysqli_stmt->execute();//发送

	$id=7;
	$blance=33;
	$mysqli_stmt->bind_param("id",$id,$blance);
	$b=$mysqli_stmt->execute();

	$id=8;
	$blance=99;
	$mysqli_stmt->bind_param("id",$id,$blance);
	$b=$mysqli_stmt->execute();

	if($b){
		die("执行成功");
	}else{
		die("执行失败".$mysqli_stmt->error);
	}
	$mysqli->close();
?>

下面是做的查询操作:

<?php
	$mysqli=new MySQLi("localhost","root","toor","test");
	if(mysqli_connect_error()){
		die(mysqli_connect_error());
	}
	$sql="select id,blance from account where id>?";
	$mysqli_stmt=$mysqli->prepare($sql);
	/*第一次查询*/
	$id=3;
	$mysqli_stmt->bind_param("i",$id);//绑定数据
	$mysqli_stmt->bind_result($id,$account);//绑定结果集
	$mysqli_stmt->execute();

	while($mysqli_stmt->fetch()){
		echo "<br/>--$id-----$account";
	}
	echo "<br/>---------------------------------";
	/*第二次查询*/
	$id=5;
	$mysqli_stmt->bind_param("i",$id);//绑定数据
	//$mysqli_stmt->bind_result($id,$account);//绑定结果集
	$mysqli_stmt->execute();

	while($mysqli_stmt->fetch()){
		echo "<br/>--$id-----$account";
	}
	echo "<br/>---------------------------------";
	/*第三次查询*/
	$id=6;
	$mysqli_stmt->bind_param("i",$id);//绑定数据
	//$mysqli_stmt->bind_result($id,$account);//绑定结果集这里已经绑定了
	$mysqli_stmt->execute();

	while($mysqli_stmt->fetch()){
		echo "<br/>--$id-----$account";
	}
	$mysqli_stmt->close();
	$mysqli->close();
?>

注:仅供参考和csdn转载。

时间: 2025-01-06 10:18:08

Mysqli的预编译机制处理批量数据过程的相关文章

js的预编译机制

1.var定义的是“当前作用域下的一个变量”,当在函数内部不使用var声明时,会被当做全局变量而不是函数内的局部变量(严格模式下还会报错) 2.js的预编辑:[对定义式函数]直接创建为作用域上的函数变量,并将其值初始化为定义的函数代码逻辑,也就是为其建立了可调用的函数变量. 3.js的预编辑:[对var定义的变量] 会在开始先全部找出来,并且将初始值设为undefined 4.[对上面代码的解释]:第一个yourname是undefined:在js开始解析时获得了第一行的yourname声明和函

PHP mysqli 扩展库(面向对象/数据库操作封装/事务控制/预编译)

1.和mysql扩展库的区别: (1   安全性.稳定性更高 (2  提供了面向对象和面向过程两种风格 2.php.ini  中的  extension=php_mysqli.dll 解除封印 3.面向对象:查询列表 1 <?php 2 3 //mysqli 操作数据(面向对象风格) 4 5 #1.创建Mysql对象 6 7 $mysqli=new MySQLi("127.0.0.1","root","daomul","test&

几个预编译指令的用法

转载:痴 狼While there is life there is hope http://www.cnblogs.com/Bolin/archive/2011/02/20/1959120.html 几个预编译指令的用法    预处理过程扫描源代码,对其进行初步的转换,产生新的源代码提供给编译器.可见预处理过程先于编译器对源代码进行处理.在C语言中,并没有任何内在的机制来完成如下一些功能:在编译时包含其他源文件.定义宏.根据条件决定编译时是否包含某些代码.要完成这些工作,就需要使用预处理程序.

JavaScript 预编译(变量提升和函数提升的原理)

本文部分内容转自https://www.cnblogs.com/CBDoctor/p/3745246.html 1.变量提升 1 console.log(global); // undefined 2 var global = 'global'; 3 console.log(global); // global 4 5 function fn () { 6 console.log(a); // undefined 7 var a = 'aaa'; 8 console.log(a); // aaa

JS运行三部曲(预编译)

JS运行的三个步骤: 语法分析 预编译 解释执行 语法分析:通俗来说就是通篇检查你的代码有没有语法错误,有语法错误的话,程序是不会执行的 解释执行:也就是程序读一句执行一句 最重点的也就是预编译了,那么预编译到底是什么?它发什么在什么时候? 先来段代码压压惊 function fn (a) { console.log(a) var a = 123; console.log(a) function a () {} console.log(a) console.log(b); var b = fun

mysql预编译处理(mysqli、PDO)

DML语句预编译: MysqLi: <?php $mysqli = new mysqli("localhost","root","root","dbname"); $mysqli->query("set names utf8"); $sql = 'insert into user(id,name,age,email) values (?,?,?,?)'; $mysqli_stmt = $mysq

PHP7预编译mysqli查询操作

//连接数据库 $mysqli = new mysqli("localhost", "root", "root", "mobilemoms"); !$mysqli->connect_error or die("CONNECT SQL ERROR".$mysqli->connect_error); $mysqli->query("set names utf8"); //s

JDBC java数据连接 读取properties 数据库连接池 预编译

1.创建连接,下载MySQL驱动(JDBC接口的实现类,就是一个jar包) public class Demo01 { public static void main(String[] args) throws ClassNotFoundException, SQLException { //1. 注册驱动 告诉虚拟机使用的是哪个数据库软件 Class.forName("com.mysql.jdbc.Driver"); //2. 获取连接对象 Connection conn = Dri

JDBC 删除数据两种方式,PreparedStatement表示预编译的 SQL 语句的对象,防止sql注入

1.statement使用的不方便 2.sql注入的问题 *  在SQL语句中使用了系统自带的关键字 or and ,让where条件判断失效 *   prepareStatement: *  1.sql语句不用在拼字符串 *  2.防止sql注入问题 1 public class CURDTest { 2 public static void main(String[] args) throws Exception { 3 //insertTest(); 4 //deleteTest(); 5