您的位置:首页 > 数据库 > MySQL

PHP学习笔记【23】--PHP数据库编程 mysqli扩展库,进行预处理数据库编程

2012-11-29 09:24 573 查看
<?php

header("Content-type:text/html;charset=utf-8");

//预编译数据库编程

//使用mysqli扩展库

//创建 mysqli对象

$mysqli=new mysqli("localhost","root","root","db_php");

$sql="insert into user values(null,?);";

//预编译处理

$mysqli_stmt=$mysqli->prepare($sql);

$name="柯南";

//绑定参数

$mysqli_stmt->bind_param("s", $name);

$b = $mysqli_stmt->execute();

$name="柯南";

$mysqli_stmt->bind_param("s", $name);

$b = $mysqli_stmt->execute();

if($b){

echo "execute success";

}else{

echo "execute error".$mysqli_stmt->error;

}

$mysqli_stmt->close();

//预编译执行查询操作

$mysqli=new mysqli("localhost","root","root","db_php");

$sql="select id,name from user";

//执行预编译

$mysqli_stmt = $mysqli->prepare($sql);

$mysqli_stmt->execute();

//绑定结果

$mysqli_stmt->bind_result($id,$name);

// 遍历结果

while ($mysqli_stmt->fetch()){

echo $id.$name."<br/>";

}

//释放资源

$mysqli_stmt->free_result();

//关闭链接

$mysqli_stmt->close();

//

?>
本文出自 “Kenan_ITBlog” 博客,请务必保留此出处http://soukenan.blog.51cto.com/5130995/1073713
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: