完成运用PDO对数据表进行写操作练习

Original 2019-04-14 11:59:17 254
abstract:增加(注意本语句需要命名占位符需要在INSERT 后面加入IGNORE

20190414111541.jpg

  1. 增加(注意本语句需要命名占位符需要在INSERT 后面加入IGNORE

prepare($sql); $name = '宋江'; $sex = '0'; $age = '58'; $email = 'zong@php.cn'; $password = sha1('aaad'); $status = '1'; $create_time = time(); $stmt->bindParam(':user_name',$name,$pdo::PARAM_STR,100); $stmt->bindParam(':sex',$sex,$pdo::PARAM_INT,2); $stmt->bindParam(':age',$age,$pdo::PARAM_INT); $stmt->bindParam(':email',$email,$pdo::PARAM_STR,200); $stmt->bindParam(':password',$password,$pdo::PARAM_STR,40); $stmt->bindParam(':status',$status,$pdo::PARAM_INT); $stmt->bindParam(':create_time',$create_time,$pdo::PARAM_INT); if($stmt->execute()){ echo ($stmt->rowCount()>0) ? '成功添加了 '.$stmt->rowCount().' 条记录' : '没有记录被添加'; }else{ exit(print_r($stmt->errorInfo(),true)); }

20190414114314.jpg

2.更新操作

prepare($sql); $name ='及时雨'; $email = 'adw@php.cn'; $id = 18; $stmt->bindParam(':user_name',$name,$pdo::PARAM_STR,100); $stmt->bindParam(':email',$email,$pdo::PARAM_STR,200); $stmt ->bindValue(':id',$id,$pdo::PARAM_INT); if($stmt->execute()){ echo ($stmt->rowCount()>0) ? '成功更新了 '.$stmt->rowCount().' 条记录' : '没有记录被更新'; }else{ echo '更新数据库失败'; exit(print_r($stmt->errorInfo(),true)); }

20190414115358.jpg

prepare($sql); $id = 5; $stmt->bindParam(':id',$id,$pdo::PARAM_INT); if($stmt->execute()){ echo ($stmt->rowCount()>0) ? '成功删除了 '.$stmt->rowCount().' 条记录' : '没有记录被删除'; }else{ echo '删除数据库失败'; exit(print_r($stmt->errorInfo(),true)); }

20190414115815.jpg

Correcting teacher:天蓬老师Correction time:2019-04-14 22:01:53
Teacher's summary:pdo预处理 , 目前是数据库操作的主流了, 没有办法,必须熟练掌握

Release Notes

Popular Entries