Blogger Information
Blog 37
fans 0
comment 0
visits 14295
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO读书操作
秋闲独醉
Original
336 people have browsed it
  1. <?php
  2. namespace space1;
  3. use \PDO;
  4. use PDOException;
  5. //对PDO二次封装
  6. class MyPDO{
  7. public $pdo;
  8. public function __construct(array $info = array())
  9. {
  10. $type = $info['type'] ?? 'mysql';
  11. $host =$info['host'] ?? '127.0.0.1';
  12. $dbname = $info['dbname'] ?? 'blog';
  13. $port = $info['port'] ?? 3308;
  14. $user = $info['username'] ?? 'root';
  15. $password = $info['password'] ?? 'root';
  16. $dsn = "$type:host=$host;dbname=$dbname;port=$port";
  17. try{
  18. $this->pdo = new PDO($dsn,$user,$password);
  19. }catch(PDOException $e){
  20. echo '数据库连接错误:'.$e->getMessage();
  21. }
  22. }
  23. }
  24. //查询功能索引数组,绑定值
  25. $obj = new MyPDO();
  26. // $sql = 'SELECT * FROM `b_user` WHERE `id` =?';
  27. // $sth = $obj->pdo->prepare($sql);
  28. // $sth->bindValue(1,4,PDO::PARAM_INT);
  29. // $sth->execute();
  30. // $result = $sth->fetchAll();
  31. // var_dump($result);
  32. //查询功能关联数组,绑定变量
  33. // $num = 4;
  34. // $sql = 'SELECT * FROM `b_user` WHERE `id` = :id';
  35. // $sth = $obj->pdo->prepare($sql);
  36. // $sth->bindParam('id',$num,PDO::PARAM_INT);
  37. // $sth->execute();
  38. // $result = $sth->fetch();
  39. // var_dump($result);
  40. //查询功能 索引数组
  41. // $sql = 'SELECT * FROM `b_user` WHERE `id` =?';
  42. // $sth = $obj->pdo->prepare($sql);
  43. // $data = [4];
  44. // $sth->execute($data);
  45. // $result = $sth->fetchAll();
  46. // var_dump($result);
  47. //查询功能 关联数组
  48. // $sql = 'SELECT * FROM `b_user` WHERE `id` =:id';
  49. // $sth = $obj->pdo->prepare($sql);
  50. // $data = ['id'=>4];
  51. // $sth->execute($data);
  52. // $result = $sth->fetchAll();
  53. // var_dump($result);
  54. //更新功能
  55. // $sql = 'UPDATE `b_user` SET u_username=:name WHERE `id`=:id';
  56. // $sth = $obj->pdo->prepare($sql);
  57. // $data = ['name' => 'Tom','id'=>4];
  58. // if($sth->execute($data)){
  59. // echo "更新成功";
  60. // }else{
  61. // echo "更新失败";
  62. // }
  63. //插入功能
  64. // $sql = <<< SQL
  65. // INSERT INTO `b_user`
  66. // (`u_username`,`u_password`,`u_reg_time`,`u_is_admin`,`key`)
  67. // VALUES(:name,:pass,:time,:is,:key);
  68. // SQL;
  69. // $sth = $obj->pdo->prepare($sql);
  70. // $sth->bindValue('name','OOO',PDO::PARAM_STR);
  71. // $sth->bindValue('pass','1234',PDO::PARAM_STR);
  72. // $sth->bindValue('time','5540349',PDO::PARAM_STR);
  73. // $sth->bindValue('is',0,PDO::PARAM_INT);
  74. // $sth->bindValue('key','5540349',PDO::PARAM_STR);
  75. // $sth->execute();
  76. // echo $obj->pdo->lastInsertId();
  77. //删除功能
  78. $sql = 'DELETE FROM `b_user` WHERE `id` = :id';
  79. $sth = $obj->pdo->prepare($sql);
  80. $sth->bindValue('id',14,PDO::PARAM_INT);
  81. $sth->execute();
  82. if($sth->columnCount()){
  83. echo "删除成功";
  84. }else{
  85. echo "删除失败";
  86. }
Correcting teacher:PHPzPHPz

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