MySQLi面向对象实现数据更新操作:
本实例连接数据库脚本引用上节课内容,不在举出,下面是页面代码:
<?php //1.连接数据库 require 'connect.php'; //2.准备SQL语句 $sql = "UPDATE staff SET birthday=? WHERE name=?;"; //3.创建预处理对象STMT $stmt = $mysqli->stmt_init(); $name = '杨过2'; $birthday = 19880510; //4.检测STMT->预处理的SQL语句对象 if (!($stmt->prepare($sql))) { exit($stmt->errno.':'.$stmt->error); } //5.执行SQL语句 $stmt->bind_param('is',$birthday,$name); $stmt->execute(); if ($stmt->affected_rows > 0) { echo "<br>更新成功".$stmt->affected_rows.'条记录'; } else { echo "<br>没有记录被更新"; }
PDO方式实现数据删除操作
<?php /** * */ //1.连接数据库,创建PDO对象 $pdo = new PDO('mysql:dbname=php','root','root'); //2.预处理 $stmt = $pdo->prepare("DELETE FROM `user` WHERE `user_id`=:user_id"); //3.执行 $stmt->execute(['user_id'=>'8']) ;