MySQLi面向对象实现更新操作
实例
<?php $dbhost='localhost'; $dbuser='root'; $dbpassword='root'; $dbname='php'; $db=new mysqli($dbhost,$dbuser,$dbpassword,$dbname); if ($db->connect_errno){ echo $db->connect_errno.':'.$db->connect_error; } $stmt=$db->prepare("UPDATE staff SET salary=3500 WHERE id=?;"); $stmt->bind_param("i",$id); $id=2; $stmt->execute(); echo '成功更新了'.$db->affected_rows.'条记录'; $stmt->close(); $db->close();
运行实例 »
点击 "运行实例" 按钮查看在线实例
PDO实现删除操作
实例
<?php //1.连接数据库,创建pdo对象 $pdo = new PDO('mysql:dbname=php','root','root'); //2.创建预处理语句对象 $stmt = $pdo->prepare("DELETE FROM staff WHERE id = :id"); //3.将参数绑定到预处理语句对象并执行 $stmt -> execute(['id'=>3]); echo '成功删除了'.$stmt->rowCount().'条记录';
运行实例 »
点击 "运行实例" 按钮查看在线实例