Blogger Information
Blog 46
fans 3
comment 2
visits 39291
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
MySQLi面向对象实现更新操作 补交2018年4月25日作业(一)
墨雨的博客
Original
760 people have browsed it

MySQLi面向对象实现更新操作

1.连接数据库服务器,建立mysqli对象

实例

<?php
//1.连接参数
$db_host = 'localhost';
$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);
}
//4.设置默认数据库
$mysqli->select_db($db_name);
//5.设置客户端默认字符编码集
$mysqli->set_charset($db_charset);

运行实例 »

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

2.mysqli面向对象更新操作  将 kc_id =18 的记录 单价(dj)更新为5.5

实例

<?php

/**
 * mysqli面向对象更新操作
 */

//1.连接数据库
require 'mysqli_connect.php';
//2.准备SQL语句
$sql = "UPDATE kc SET dj = ? WHERE kc_id = ?;";
//3.创建STMT预处理对象
$stmt = $mysqli->stmt_init();
//4.检测预处理SQL语句
if ($stmt->prepare($sql)) {
    
    //绑定参数到预处理SQL语句
    $stmt->bind_param('di',$dj,$kc_id);
    $dj = 5.5;
    $kc_id = 18;
     //执行预处理语句
     $stmt->execute();
     //检测运行结果
     if ($stmt->affected_rows > 0 ){
         echo '成功更新'.$stmt->affected_rows.'条记录';
     } else {
         echo '没有记录被更新';
     }
    //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