博客列表 >Mysqli 预处理-更新、删除操作 20180425-11:43

Mysqli 预处理-更新、删除操作 20180425-11:43

AsNwds_MIS王的博客
AsNwds_MIS王的博客原创
2018年04月25日 11:44:53664浏览

实例

<?php
/医院
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/25
 * Time: 10:17
 */
//1、连接数据库
require 'mysqli_config.php';
//2、执行查询
$con = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);

$sql = "update user set user_name = ? where user_id = 1  ";
$user_name = '王老师4';
//创建对象
$stmt = mysqli_stmt_init($con);

if (mysqli_stmt_prepare($stmt,$sql)){
    //参数绑定
    mysqli_stmt_bind_param($stmt,"s",$user_name);
    //执行语句
    mysqli_stmt_execute($stmt);
    echo '<br>更新了'.mysqli_stmt_affected_rows($stmt).'条记录'.mysqli_stmt_insert_id($stmt);
}
//注销对象
mysqli_stmt_close($stmt);
//关闭数据库
mysqli_close($con);

运行实例 »

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

删除:

实例

<?php
/医院
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/25
 * Time: 11:46
 */
require 'mysqli_config.php';
//2、执行查询
$con = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);

$sql = "delete from user where user_id=?   ";

//创建对象
$stmt = mysqli_stmt_init($con);

if (mysqli_stmt_prepare($stmt,$sql)){
    //执行语句
    $user_id = 1;
    mysqli_stmt_execute($stmt);
    echo '<br>更新了'.mysqli_stmt_affected_rows($stmt).'条记录';
}
//注销对象
mysqli_stmt_close($stmt);
//关闭数据库
mysqli_close($con);

运行实例 »

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


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议