php mysql 預編譯

WBOY
發布: 2016-08-08 09:25:23
原創
2214 人瀏覽過

預編譯執行 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();
	
?>
登入後複製

reee

以上就介紹了php mysql 預編譯,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板