欢迎投稿

今日深度:

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

Mysqli的预编译机制处理批量数据过程,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转载。

www.htsjk.Com true http://www.htsjk.com/shujukunews/6858.html NewsArticle Mysqli的预编译机制处理批量数据过程,mysqli编译机制 mysqli增强,还有一部分是对事物处理机制和预编译机制的增加,其实这两者都是为安全和执行效率着想的,这里主要说一下mysqli的预...
评论暂时关闭