<?php
//设置连接参数
define('DB_HOST','127.0.0.1');//数据库服务器地址
define('DB_USER','root');//用户名
define('DB_PASS','root');//密码
define('DB_NAME','test');//数据库名
define('DB_CHAR','utf8');//字符集
//获取连接对象
$db = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die();
//准备sql模板
$sql_ins = "INSERT IGNORE user SET USER_NAME=? , USER_AGE=?;";//新增
$sql_del = "DELETE FROM user WHERE USER_NAME = ? ";
$sql_upd = "UPDATE user SET USER_AGE=? WHERE USER_NAME = ?;";
//创建stmt对象
$stmt = mysqli_stmt_init($db);
//新增
//检测语句
//if(mysqli_stmt_prepare($stmt,$sql_ins)){
// //绑定变量到语句中的占位符
// mysqli_stmt_bind_param($stmt,'si',$name,$age);//i,s,d
// $name = '玛法里奥';
// $age = '200';
// //执行sql
// mysqli_stmt_execute($stmt);
//}else{
// exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt));
//}
//删除
//检测语句
//if(mysqli_stmt_prepare($stmt,$sql)){
// //绑定变量到语句中的占位符
// mysqli_stmt_bind_param($stmt,'s',$name);//i,s,d
// $name = '玛法里奥';
// //执行sql
// mysqli_stmt_execute($stmt);
//}else{
// exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt));
//}
//更新
//检测语句
if(mysqli_stmt_prepare($stmt,$sql_upd)){
//绑定变量到语句中的占位符
mysqli_stmt_bind_param($stmt,'is',$age,$name);//i,s,d
$name = '玛法里奥';
$age = '600';
//执行sql
mysqli_stmt_execute($stmt);
}else{
exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt));
}
//注销stmt
mysqli_stmt_close($stmt);
//关闭连接
mysqli_close($db);