Blogger Information
Blog 36
fans 2
comment 0
visits 23617
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
2019年11月21号--pdo操作:连接、增删查改操作
Rambo-Yang
Original
580 people have browsed it

1. pdo操作:连接、增删查改操作

  • pdo连接数据库

  1. <?php
  2. $db = [
  3. 'type' => 'mysql',
  4. 'host' => 'localhost',
  5. 'dbname' => 'movies',
  6. 'username' => 'root',
  7. 'password' => 'root'
  8. ];
  9. //配置数据源DSN信息 注意不能有空格
  10. $dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}";
  11. try{
  12. //连接数据库
  13. $pdo = new PDO($dsn,$db['username'],$db['password']);
  14. }catch(PDOException $e){
  15. //输出错误信息
  16. die($e->getMessage());
  17. }
  • 新增记录

  1. //1,准备SQL语句 :n 是占位符
  2. $sql ='INSERT INTO `category` SET `name` = :n,`alias` = :alias';
  3. //2,创建预处理对象
  4. $stmt = $pdo -> prepare($sql);
  5. //3,绑定数据
  6. $n = 'gt';
  7. $alias = '港台大片';
  8. $stmt->bindParam('n',$n,PDO::PARAM_STR);
  9. //4,执行sql操作
  10. $add = $stmt->execute();
  11. //5,判断受影响的行数
  12. if($add){
  13. if($stmt->rowCount() > 0){
  14. echo '成功新增了'.$stmt->rowCount().'条记录';
  15. }
  16. }else{
  17. die(print_r($stmt->errorInfo(),true));
  18. }
  19. //6,关闭PDO连接
  20. $pdo = null;
  • 删除操作

  1. $sql = 'DELETE FROM `category` WHERE `cate_id` = :id';
  2. $stmt = $pdo->prepare($sql);
  3. $id = 4;
  4. $stmt->bindParam('id',$id,PDO::PAMARM_INT);
  5. $del = $stmt->execute();
  6. if($del){
  7. if($stmt->rowCount() >0){
  8. echo '成功删除了'..$stmt->rowCount().'条记录';
  9. }
  10. }else{
  11. die(print_r($stmt->errorInfo(),true));
  12. }
  • 修改操作

  1. $sql = 'UPDAATE `category` SET `name` = :n,`alias` = :alias WHERE `cate_id` = :id';
  2. $stmt = $pdo->prepare($sql);
  3. $n = 'xmt';
  4. $alias = '新马泰大片';
  5. $cate_id = 4;
  6. $stmt->bindParam('n',$n,PDO::PARAM_STR);
  7. $stmt->bindParam('alias',$alias,PDO::PARAM_STR);
  8. $stmt->bindParam('id',$id,PDO::PARAM_INT);
  9. $res = $stmt->execute();
  10. if($res){
  11. if($stmt->rowCount() >0){
  12. echo '成功修改了'.$stmt->rowCount().'条记录';
  13. }
  14. }else{
  15. die(print_r($stmt->errorInfo(),true));
  16. }
  • 查询数据

  1. $sql = 'SELECT * FROM `category` WHERE `cate_id` = :id';
  2. $stmt = $pdo -> prepare($sql);
  3. $id=4;
  4. $stmt->bindParam('id',$id,PDO::PARAM_INT);
  5. $sel = $stmt->execute();
  6. //fetch() 获取结果集中的一行
  7. //PDO::FETCH_ASSOC 获取关联数组部分
  8. while($a = $stmt->fetch(PDO::FETCH_ASSOC))
  9. {
  10. echo print_r($a,true);
  11. }
  12. //获取结果集
  13. foreach($stmt->fetchAll(PDO::FETCH_ASSOC) AS $V){
  14. echo print_r($v,true);
  15. }





Correcting teacher:查无此人查无此人

Correction status:qualified

Teacher's comments:完成的不错,继续加油。
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post