zoukankan      html  css  js  c++  java
  • PHP mysqli扩展库 预处理技术

    1、使用mysqli扩展库 预处理技术 mysqli stmt 向数据库添加3个用户

    <?php
    
        //mysqli扩展库 预处理技术 mysqli stmt 向数据库添加3个用户
        //1、创建mysqli对象
        $mysqli = new MySQLi("localhost","root","root","test");
        if($mysqli->connect_error){
            die($mysqli->conncet_error);
        }
        //2、创建预编译对象
        $sql="insert into user1(name,password,email,age) values(?,?,?,?)";
        $mysqli_stmt=$mysqli->prepare($sql);
    
        //绑定参数
        $name="小芳";
      //$password=md5("ffff"); $password
    ="123456"; $email="xiaofang@126.com"; $age=18; //参数绑定->给?号赋值 这里类型和顺序要一致 $mysqli_stmt->bind_param("sssi",$name,$password,$email,$age); //执行 $b=$mysqli_stmt->execute(); //继续添加 $name="小杨"; $password="123456"; $email="xiaoyang@126.com"; $age=18; //参数绑定->给?号赋值 这里类型和顺序要一致 $mysqli_stmt->bind_param("sssi",$name,$password,$email,$age); //执行 $b=$mysqli_stmt->execute(); //继续添加 $name="小G"; $password="123456"; $email="xiaoG@126.com"; $age=18; //参数绑定->给?号赋值 这里类型和顺序要一致 $mysqli_stmt->bind_param("sssi",$name,$password,$email,$age); //执行 $b=$mysqli_stmt->execute(); if(!$b){ echo "操作失败".$mysqli_stmt->error; }else{ echo "操作成功"; } //关闭预编译 $mysqli_stmt->close(); $mysqli->close(); ?>

    2、使用预处理查询id>5的用户id name email

    <?php
    
        //使用预处理查询id>5的用户id name email
        $mysqli=new MySQLi("localhost","root","root","test");
        if($mysqli->connect_error){
            die($mysqli->connect_error);
        }
    
        //创建预编译对象
        $sql="select id,name,email from user1 where id>?";
        $mysqli_stmt=$mysqli->prepare($sql);
        
        $id=5;
        //绑定参数
        $mysqli_stmt->bind_param("i",$id);
        //绑定结果集
        $mysqli_stmt->bind_result($id,$name,$email);
        //执行
        $mysqli_stmt->execute();
    
        //取出绑定的值
        while($mysqli_stmt->fetch()){
            echo "<br/>$id--$name--$email";
        }
        
        //关闭资源
        //释放结果
        $mysqli_stmt->free_result();
        //关闭与编译语句
        $mysqli_stmt->close();
        //关闭连接
        $mysqli->close();
    
    ?>
  • 相关阅读:
    R语言中获取当前目录
    Error : .onLoad failed in loadNamespace() for 'rJava', details: call: inDL(x, as.logical(local), as.logical(now), ...) error: 无法载入共享目标对象‘D:/Program Files/R/R-3.2.2/library/rJava/libs/x64/rJava.dll
    Ubuntu打开系统监视器
    Myeclipse中js总是报错
    ubuntu 卸载 google-chrome
    ubuntu下安装myeclipse+破解
    bzoj2085-POI2010-Hamsters
    bzoj1061-[Noi2008]志愿者招募-单纯形 & 费用流
    bzoj2716-天使玩偶
    bzoj3779-重组病毒
  • 原文地址:https://www.cnblogs.com/pwm5712/p/3054539.html
Copyright © 2011-2022 走看看