Home  >  Article  >  Backend Development  >  php mysql precompiled

php mysql precompiled

WBOY
WBOYOriginal
2016-08-08 09:25:232209browse

Pre-compile and execute dml statements

<?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();
?>

Pre-compile and execute dql statements:

<?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();
	
?>

The above introduces php mysql pre-compilation, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn