Blogger Information
Blog 33
fans 0
comment 0
visits 24492
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
MySQLi面向对象的预处理技术实现更新操作--2018.05.02日上传
张鑫的博客
Original
734 people have browsed it

多写多练多想,数据库增删改查重点放在后面的PDO技术上

数据库连接代码:


实例

<?php
/**
 * 连接数据库
 */

//1.连接参数
$db_host = '127.0.0.1';
$db_user = 'root';
$db_pass = 'root';
$db_name = 'php';
$db_charset = 'utf8';

//2.连接数据库服务器,并返回mysqli对象
$mysqli = @new mysqli($db_host,$db_user,$db_pass);

//3.检测是否连接成功
if ($mysqli->connect_errno) {
    exit('连接错误'.$mysqli->connect_errno.':'.$mysqli->connect_error);
}

//echo '<h1>连接成功</h1>';

//4.设置默认数据库
$mysqli->select_db($db_name);

//5.设置客户端默认字符编码集
$mysqli->set_charset($db_charset);

运行实例 »

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

更新操作代码:


实例

<?php
/**
 * 新增操作
 */

//1.连接数据库
require 'mysql_connect.php';

//2.准备SQL语句
$sql = "UPDATE user SET email=? WHERE user_id=?;";

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

//4.检测预处理SQL语句
if ($stmt->prepare($sql)) {
    //绑定参数到预处理SQL语句
    $stmt->bind_param('si',$email,$id);

    
    //设置参数
    $id = 2;
    $email = 'tutu@qq.com';
    //执行预处理语句
    $stmt->execute();
    if ($stmt->affected_rows > 0 ){
        echo '<br>成功更新了'.$stmt->affected_rows.'条记录';
    } else {
        echo '<br>没有记录被更新';
    }

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

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