Blogger Information
Blog 64
fans 2
comment 1
visits 46867
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
MySQLi面向对象实现更新操作——2018年4月26日
Y的博客
Original
603 people have browsed it

效果图:

360截图16410122537237.jpg

代码:

实例

<?php
//1.连接数据库
require 'connect.php';

//2.准备SQL语句
$sql = "UPDATE staff SET name=? WHERE id=? ;";

//3.创建STMT预处理对象
$stmt = $mysqli->stmt_init();

//4.检测预处理SQL语句
if ($stmt->prepare($sql)) {

    //用二维数组来保存要更新的记录
    $data[] = ['name'=> '张无忌','id'=>1];
    $data[] = ['name'=> '赵敏','id'=>2];
    $data[] = ['name'=> '周芷若','id'=>3];

    //绑定参数到预处理SQL语句
    $stmt->bind_param('si',$name,$id);

    foreach ($data as $staff) {
        //准备数据
        $name = $staff['name'];
        $id = $staff['id'];
        //执行预处理语句
        $stmt->execute();
        //检测运行结果
        if ($stmt->affected_rows > 0 ){
            echo '<br>更新成功'.$stmt->affected_rows.'条记录,更新主键id是:'.$id;
        } else {
            echo '<br>没有更新记录';
        }
    }

    //5.注销stmt对象
    $stmt->close();
} else {
    exit($stmt->errno.':'.$stmt->error);
}

//6. 关闭数据库连接
$mysqli->close();

运行实例 »

点击 "运行实例" 按钮查看在线实例

总结:

  1. 连接数据库:require‘connect.php’;

  2. 执行SQL语句:UPDATE  SET  WHERE;

  3. 创建STMT对象:$stmt = $mysqli->stmt_init();

  4. 检测预处理SQL语句:if( $stmt->prepare($stmt));

  5. 注销stmt对象: $stmt->close();

  6. 关闭数据库:$mysqli->close();

Correction status:Uncorrected

Teacher's comments:
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