Blogger Information
Blog 34
fans 2
comment 0
visits 23182
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月28号 创建数据库读写操作类
遗忘了寂寞
Original
620 people have browsed it

类与对象

  1. <?php
  2. class db{
  3. public $dsn;
  4. public $username;
  5. public $password;
  6. public $pdo;
  7. //构造方法,设置pdo连接参数
  8. public function __construct($dsn,$username,$password){
  9. $this->dsn=$dsn;
  10. $this->username=$username;
  11. $this->password=$password;
  12. $this->connect();
  13. }
  14. //数据连接方法
  15. public function connect(){
  16. try {
  17. //实例化PDO类,创建PDO对象
  18. $this->pdo=new PDO($this->dsn,$this->username,$this->password);
  19. } catch (PDOException $e) {
  20. die('数据库错误:'.$e->getMessage());
  21. }
  22. return $pdo;
  23. }
  24. //查询方法 传参说明(表名,字段名或字段名数组,查询条件,排序条件,分页条件)
  25. public function select($table,$fields, $where='', $order='',$limit=''){
  26. //连接pdo
  27. $this->connect();
  28. //创建SQL语句
  29. $sql = 'SELECT ';
  30. if (is_array($fields)) {
  31. foreach ($fields as $field) {
  32. $sql .= $field.', ';
  33. }
  34. } else {
  35. $sql .= $fields;
  36. }
  37. $sql = rtrim(trim($sql),',');
  38. $sql .= ' FROM '.$table;
  39. //查询条件
  40. if(!empty($where)){
  41. $sql .= ' WHERE '.$where;
  42. }
  43. //排序条件
  44. if(!empty($order)) {
  45. $sql .= ' order by '.$order;
  46. }
  47. //分页条件
  48. if(!empty($limit)) {
  49. $sql .= ' limit '.$limit;
  50. }
  51. $sql .= ';';
  52. //创建PDO预处理对象
  53. $stmt = $this->pdo->prepare($sql);
  54. //执行查询操作
  55. if($stmt->execute()){
  56. if($stmt->rowCount()>0){
  57. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  58. //返回一个二维数组
  59. return $stmt->fetchAll();
  60. }
  61. } else {
  62. return false;
  63. }
  64. }
  65. //插入数据方法 传参说明(表名,值(数组[字段名=>值]))
  66. function insert($table,$data=[]){
  67. //连接pdo
  68. $this->connect();
  69. //创建SQL语句
  70. $sql = "INSERT INTO {$table} SET ";
  71. //组装插入语句
  72. if(is_array($data)){
  73. foreach ($data as $k=>$v) {
  74. $sql .= $k.'="'.$v.'", ';
  75. }
  76. }else{
  77. return false;
  78. }
  79. //去掉尾部逗号,并添加分号结束
  80. $sql = rtrim(trim($sql),',').';';
  81. //创建PDO预处理对象
  82. $stmt = $this->pdo->prepare($sql);
  83. //执行新增操作
  84. if($stmt->execute()){
  85. if($stmt->rowCount()>0){
  86. return true;
  87. }
  88. } else {
  89. return false;
  90. }
  91. }
  92. //更新数据方法 传参说明(表名,值(数组[字段名=>值]),条件)
  93. function update($table,$data=[], $where='') {
  94. //连接pdo
  95. $this->connect();
  96. //创建SQL语句
  97. $sql = "UPDATE {$table} SET ";
  98. //组装修改语句
  99. if(is_array($data)){
  100. foreach ($data as $k=>$v) {
  101. $sql .= $k.'="'.$v.'", ';
  102. }
  103. }
  104. //去掉尾部逗号,并添加分号结束
  105. $sql = rtrim(trim($sql),',');
  106. //查询条件
  107. if(!empty($where)){
  108. $sql .= ' WHERE '.$where;
  109. }
  110. //创建PDO预处理对象
  111. $stmt = $this->pdo->prepare($sql);
  112. //执行新增操作
  113. if($stmt->execute()){
  114. if($stmt->rowCount()>0){
  115. return true;
  116. }
  117. } else {
  118. return false;
  119. }
  120. }
  121. //删除数据方法 传参说明(表名,条件)
  122. function delete($table,$where=''){
  123. //连接pdo
  124. $this->connect();
  125. //创建SQL语句
  126. $sql = "DELETE FROM {$table} ";
  127. //查询条件
  128. if(!empty($where)){
  129. $sql .= ' WHERE '.$where;
  130. }
  131. //创建PDO预处理对象
  132. $stmt = $this->pdo->prepare($sql);
  133. //执行删除操作
  134. if($stmt->execute()){
  135. if($stmt->rowCount()>0){
  136. return true;
  137. }
  138. } else {
  139. return false;
  140. }
  141. }
  142. }
  143. //创建对像
  144. $db=new db('mysql:host=localhost;dbname=tj_ys','root','123123');
  145. // var_dump($db);
  146. // 调用查询数据方法
  147. $user=$db->select('user','*','','','');
  148. print_r($user);
  149. // 调用插入数据方法
  150. // $db->insert('user',['phone'=>'13390773018','name'=>'张三','pwd'=>md5('123123'),'age'=>'22']);
  151. // 调用更新数据方法
  152. // $db->update('user',['pwd'=>md5('123123'),'age'=>'25'],'uid=19');
  153. // 调用删除数据方法
  154. // $db->delete('user','uid=19');
  155. ?>

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