mysql预处理语句,执行删除和更新。init->prepare->bind_param->excute.
config.php
实例
<?php define('DB_HOST', 'localhost'); define('DB_USER', 'root'); define('DB_PASSWORD', 'root'); define('DB_NAME','user' ); define('DB_CHAR', 'utf8'); // 抑制错误信息 $db=@mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die("连接数据库失败"); mysqli_set_charset($db,DB_CHAR);
运行实例 »
点击 "运行实例" 按钮查看在线实例
update_stmt.php
实例
<?php /** */ require 'config.php'; $stmt=mysqli_stmt_init($db); $sql="UPDATE staf SET salary=? WHERE staff_id=3;"; if (mysqli_stmt_prepare($stmt,$sql)){ mysqli_stmt_bind_param($stmt,'i',$salary); $salary=7000; //执行 mysqli_stmt_execute($stmt); } else { exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt)); } //注销stmt mysqli_stmt_close($stmt); mysqli_close($db);
运行实例 »
点击 "运行实例" 按钮查看在线实例
delete_stmt.php
实例
<?php /** * */ require 'config.php'; //初始化stmt $stmt=mysqli_stmt_init($db); //检测sql语句 $sql="DELETE FROM staf WHERE staff_id=?;"; if (mysqli_stmt_prepare($stmt,$sql)) { mysqli_stmt_bind_param($stmt,'i',$id); $id=7; mysqli_stmt_execute($stmt); } else { exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt)); } mysqli_stmt_close($stmt); mysqli_close($db);
运行实例 »
点击 "运行实例" 按钮查看在线实例