1.mysqli面向对象实现更新:
实例
<?php require 'mysqli_con.php'; $sql = "UPDATE IGNORE staff SET age = ?, salary = ? WHERE staff_id = ?;"; $stmt = $mysqli->stmt_init(); //创建对象 //$stmt = mysqli_stmt_init($db); if($stmt->prepare($sql)){ $data[] = ['age'=>'42','salary'=>'6000','staff_id'=>'2']; $data[] = ['age'=>'30','salary'=>'5000','staff_id'=>'6']; $stmt->bind_param('iii',$age,$salary,$staff_id); foreach($data as $staff){ $age = $staff["age"]; $salary = $staff["salary"]; $staff_id = $staff["staff_id"]; $stmt->execute(); if($stmt->affected_rows > 0){ echo 'new update:'.$stmt->affected_rows; }else{ echo 'no update'; } } $stmt->close(); }else{ exit($stmt->errno().':'.$stmt->error()); } $mysqli->close();
运行实例 »
点击 "运行实例" 按钮查看在线实例
2.PDO实现删除
实例
<?php //1.连接数据库,创建pdo对象 $pdo = new PDO('mysql:dbname=php','root','root'); //2.创建预处理语句对象 $stmt = $pdo->prepare("DELETE FROM staff WHERE staff_id=:staff_id"); //3.将参数绑定到预处理语句对象并执行 $stmt -> execute(['staff_id'=>6]); echo 'new delete:'.$stmt->rowCount();
运行实例 »
点击 "运行实例" 按钮查看在线实例