Blogger Information
Blog 22
fans 0
comment 0
visits 17787
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0103预处理删除操作
yestrue的博客
Original
743 people have browsed it
<?php

/**
 * 预处理删除
 */
//连接数据库,并创建连接对象$mysqli
require 'public/connect.php';
$sql2 = "SELECT id,name,email FROM user WHERE id=?";
$data2 = 5;
$mysqli_stmt2 = $mysqli->prepare($sql2);
$mysqli_stmt2->bind_param('i',$data2);

if ($mysqli_stmt2->execute()) {
	$mysqli_stmt2->store_result();
    //尽管结果集对象存在,但有可能是空的,所以有必要进行非空判断
    $num = $mysqli_stmt2->num_rows;  //获取到结果集中的记录数量
    if ($num == 1) {  //如果找到了,并且只有一条符合条件的记录,才允许更新
        //1. 先获取到当前记录更新前的信息,保存到变量$row中
         $mysqli_stmt2->bind_result($id,$name,$email);
        $row = $mysqli_stmt2->fetch();
        //2.先创建要更新的数据,临时保存到一个数组中
        $data = ['name'=>'皮特', 'email'=>'z@test.cn', 'password'=>'123'];
        //3.创建更新的预处理SQL语句
        $sql = "DELETE FROM user WHERE id=?";
        //4.用连接对象$mysqli的prepare()方法来创建一个预处理对象
        $mysqli_stmt = $mysqli->prepare($sql);
        //5.调用预处理对象中的bind_param()方法将实际参数与SQL语句中的占位符进行绑定
        $mysqli_stmt->bind_param('i',$id );
        //6. 执行预处理更新操作: execute(),返回布尔值,成功true,失败为false
        if($mysqli_stmt->execute()) {
            //如果更新成功,应该根据受影响的记录数量,再进行一次判断
            if ($mysqli_stmt->affected_rows) {  //如果更新成功,会返回整数: 1
                echo '<h3>删除成功</h3>';
            }else {
                echo '<h3 style="color:red">没有记录被删除</h3>';
            }

        } else {
            echo '<h3 style="color:red">删除失败'.$mysqli_stmt->error.'</h3>';
        }

        //释放结果集
        // $mysqli_result->free();
        //关闭预处理语句
        $mysqli_stmt->close();

    } else {
        echo '<h3 style="color:red">数据表中没有查询到需要删除的记录</h3>';
    }



} else {
    echo '<h3 style="color:red">查询失败,请检查~~'.$mysqli->error.'</h3>';
}

//关闭连接
$mysqli->close();


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post