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

来源:互联网 发布:java爬虫 zol壁纸 编辑:程序博客网 时间:2024/05/21 09:13

<?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

原创粉丝点击