php mysql 预编译语句_php mysql 预编译
預編譯執行 dml 語句
//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
";
}
//繼續添加
$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
";
}
//繼續添加
$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
";
}
$mysqli->close();
?>
預編譯執行 dql 語句:
//預編譯 執行 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
";
}
$mysqli_stmt->close();
$mysqli->close();
?>
總結
以上是生活随笔為你收集整理的php mysql 预编译语句_php mysql 预编译的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php mysql 随机排序函数_php
- 下一篇: mysql主从克隆服务器_mysql主从