php mysql 预编译

WBOY
发布: 2016-08-08 09:25:23
原创
2203 人浏览过

预编译执行 dml 语句

<?php //mysql预编译
	$mysqli = new mysqli("localhost", "root", "root", "php");
	
	$mysqli->query("set names gbk");
	
	$sql = "insert into user1(name,psw,email,age) values(?,?,?,?)";
	
	$mysqli_stmt = $mysqli->prepare($sql);
	
	//绑定参数
	
	$name = "公子玮";
	$psw = "123";
	$email = "gonziwei.sohu,com";
	$age = 24;

	$mysqli_stmt->bind_param("sssi",$name,$psw,$email,$age);
	
	$b = $mysqli_stmt->execute();
	
	if(!$b){
		die("failed".$mysqli_stmt->error);
		exit();
	}else{
		echo "success<br>";
	}
	
	
	//继续添加
	$name = "编程大师";
	$psw = "123";
	$email = "dashi.sohu,com";
	$age = 25;
	
	$mysqli_stmt->bind_param("sssi",$name,$psw,$email,$age);
	
	$b = $mysqli_stmt->execute();
	
	if(!$b){
		die("failed".$mysqli_stmt->error);
		exit();
	}else{
		echo "success<br>";
	}
	
	//继续添加
	$name = "编程屌丝";
	$psw = "123";
	$email = "diaoshi.sohu,com";
	$age = 25;
	
	$mysqli_stmt->bind_param("sssi",$name,$psw,$email,$age);
	
	$b = $mysqli_stmt->execute();
	
	if(!$b){
		die("failed".$mysqli_stmt->error);
		exit();
	}else{
		echo "success<br>";
	}
	
	$mysqli->close();
?>
登录后复制

预编译执行 dql 语句:

<?php //预编译 执行 dql 语句
	header("Content-type:text/html;charset=utf-8");
	$mysqli = new mysqli("localhost", "root", "root", "php");
	
	if($mysqli->connect_error){
		die($mysqli->connect_error);
		exit();
	}
	//create a predefined object and get a position
	
	$sql = "select id,name,email from user1 where id > ?";
	
	$mysqli_stmt = $mysqli->prepare($sql);
	$id = 5;
	//bind param
	$mysqli_stmt->bind_param("i",$id);
	
	//bind results
	
	$mysqli_stmt->bind_result($id,$name,$email);
	
	
	$mysqli_stmt->execute();
	
	while ($mysqli_stmt->fetch()){
		echo "--$id--$name--$email<br>";
	}
	
	$mysqli_stmt->close();
	
	$mysqli->close();
	
?>
登录后复制

以上就介绍了php mysql 预编译,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!