Blogger Information
Blog 38
fans 0
comment 0
visits 29560
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0424,mysqli预处理语句
riskcn的博客
Original
743 people have browsed it

预处理流程:

连接数据库

准备sql语句

创建stmt对象(mysqli_stmt_init(DB))

检测sql语句(mysqli_stmt_prepare(STMT,SQL))

绑定预处理元素(mysqli_stmt_bind_param(STMT,TYPE,OBJ))

提交预处理(mysqli_stmt_execute(STMT))

注销对象(mysqli_stmt_close(STMT))

关闭连接(mysqli_close(DB))

UPDATE操作

实例

<?php
header("Content-type: text/html; charset=utf-8");
// 1.连接数据库
require "connect.php";

// 2.准备sql语句
$sql="UPDATE user SET name=?,age=?,salary=? WHERE id=4" ;
//3.创建stmt对象
$stmt=mysqli_stmt_init($conn);
//3.检测sql语句
if(mysqli_stmt_prepare($stmt,$sql)){
    //绑定预处理元素
    mysqli_stmt_bind_param($stmt,'sii',$name,$age,$salary);
    $name="渣渣辉";
    $age=23;
    $salary=1600;

    //执行预处理
    mysqli_stmt_execute($stmt);
    echo "影响了:".mysqli_stmt_affected_rows($stmt)."条记录";
}else{
//    错误信息打印
    exit(mysqli_stmt_errno($stmt).":".mysqli_stmt_error($stmt));
}
//注销stmt对象
mysqli_stmt_close($stmt);
//关闭连接t
mysqli_close($conn);

运行实例 »

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

QQ截图20180425102859.png

DELETE操作:流程基本一致

实例

<?php
header("Content-type: text/html; charset=utf-8");
//创建数据库链接
require "connect.php";
//创建sql语句
$sql="DELETE FROM user WHERE name=?;";
//创建stmt对象
$stmt=mysqli_stmt_init($conn);
//检查sql语句
if(mysqli_stmt_prepare($stmt,$sql)){
//    绑定sql元素
    mysqli_stmt_bind_param($stmt,'s',$name);
    $name='渣渣辉';
//    执行预处理
    mysqli_stmt_execute($stmt);
//    返回处理结果
    echo "删除了:".mysqli_stmt_affected_rows($stmt)."行";
}else{
exit (mysqli_stmt_errno($stmt).":".mysqli_stmt_error($stmt));
}
mysqli_stmt_close($stmt);
mysqli_close($conn);

运行实例 »

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


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