<?php header('Content-Type:text/html;charset=UTF-8'); *try { $dsn = 'mysql:dbname=demo'; $userNmae = 'root'; $password = 'root'; $pdo = new PDO($dsn,$userNmae,$password); // $sql = "DELETE FROM `user` WHERE `id`=:id"; //执行预处理sql删除语句,创建一个PDOStatement对象 $pdoStmt = $pdo->prepare($sql); if ($pdoStmt == true) { //声明要删除的id $data = ['id'=>4]; //绑定要删除的id $pdoStmt->bindParam(':id',$data['id']); $res = $pdoStmt->execute($data); if ($res == true) { echo '删除成功!'; } else { echo '删除失败!!!'; } } else { print_r($pdo->errorInfo()); } } catch (PDOException $e) { print 'Connect ERROR'.$e->getMessage(); dre(); }
简写:
<?php header('Content-Type:text/html;charset=UTF-8'); //连接数据库 $pdo = new PDO('mysql:dbname=demo','root','root'); //执行sql删除语句 $pdoStmt = $pdo->prepare("DELETE FROM `user` WHERE `id`=:id"); //执行pdo对象并判断结果 echo $pdoStmt->execute(['id'=>13]) ? '删除成功!' : '删除失败!!!';