Blogger Information
Blog 27
fans 0
comment 0
visits 17355
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
023-11月27日-PHP第13节-类自动加载、继承
冇忉丼
Original
583 people have browsed it

1. 把 php公用方法库中的 数据库操作函数, 重写到 Db类中。

  1. class DB{
  2. public $dsn;
  3. public $user;
  4. public $password;
  5. public $pdo;
  6. public $table;
  7. // public $table;
  8. // public $fields;
  9. // public $where;
  10. // public $order;
  11. // public $limit;
  12. // public $stmt;
  13. public function __construct($dsn,$user,$password,$table)
  14. {
  15. $this->dsn = $dsn;
  16. $this->user = $user;
  17. $this->password = $password;
  18. $this->table = $table;
  19. $this->connect();
  20. //已经预处理,后面函数不用添加此语句了
  21. }
  22. public function connect(){
  23. $this->pdo = new PDO($this->dsn,$this->user,$this->password);
  24. }
  25. public function select($fields, $where='', $order='',$limit=''){
  26. // $this->table = $table;
  27. $sql = 'SELECT ';
  28. if (is_array($fields)) {
  29. foreach ($fields as $field) {
  30. $sql .= $field.', ';
  31. }
  32. } else {
  33. $sql .= $fields;
  34. }
  35. $sql = rtrim(trim($sql),',');
  36. $sql .= ' FROM '.$this->table;
  37. //查询条件
  38. if(!empty($where)){
  39. $sql .= ' WHERE '.$where;
  40. }
  41. //排序条件
  42. if(!empty($order)) {
  43. $sql .= ' order by '.$order;
  44. }
  45. //分页条件
  46. if(!empty($limit)) {
  47. $sql .= ' limit '.$limit;
  48. }
  49. $sql .= ';';
  50. //创建PDO预处理对象
  51. $stmt = $this->pdo->prepare($sql);
  52. //执行查询操作
  53. if($stmt->execute()){
  54. if($stmt->rowCount()>0){
  55. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  56. //返回一个二维数组
  57. return $stmt->fetchAll();
  58. }
  59. } else {
  60. return false;
  61. }
  62. }
  63. public function find($fields,$where=''){
  64. $sql = 'SELECT ';
  65. if (is_array($fields)) {
  66. foreach ($fields as $field) {
  67. $sql .= $field.', ';
  68. }
  69. } else {
  70. $sql .= $fields;
  71. }
  72. $sql = rtrim(trim($sql),',');
  73. $sql .= ' FROM '.$this->table;
  74. //查询条件
  75. if(!empty($where)){
  76. $sql .= ' WHERE '.$where;
  77. }
  78. $sql .= ' LIMIT 1;';
  79. //创建PDO预处理对象
  80. $stmt = $this->pdo->prepare($sql);
  81. //执行查询操作
  82. if($stmt->execute()){
  83. if($stmt->rowCount()>0){
  84. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  85. return $stmt->fetch();
  86. }
  87. } else {
  88. return false;
  89. }
  90. }
  91. public function insert($data=[]){
  92. $sql = "INSERT INTO {$this->table} SET ";
  93. //组装插入语句
  94. if(is_array($data)){
  95. foreach ($data as $k=>$v) {
  96. $sql .= $k.'="'.$v.'", ';
  97. }
  98. }else{
  99. return false;
  100. }
  101. //去掉尾部逗号,并添加分号结束
  102. $sql = rtrim(trim($sql),',').';';
  103. //创建PDO预处理对象
  104. $stmt = $this->pdo->prepare($sql);
  105. //执行新增操作
  106. if($stmt->execute()){
  107. if($stmt->rowCount()>0){
  108. return true;
  109. }
  110. } else {
  111. return false;
  112. }
  113. }
  114. public function update($data=[], $where=''){
  115. $sql = "UPDATE {$this->table} SET ";
  116. //组装修改语句
  117. if(is_array($data)){
  118. foreach ($data as $k=>$v) {
  119. $sql .= $k.'="'.$v.'", ';
  120. }
  121. }
  122. //去掉尾部逗号,并添加分号结束
  123. $sql = rtrim(trim($sql),',');
  124. //查询条件
  125. if(!empty($where)){
  126. $sql .= ' WHERE '.$where;
  127. }
  128. //创建PDO预处理对象
  129. $stmt = $this->pdo->prepare($sql);
  130. //执行新增操作
  131. if($stmt->execute()){
  132. if($stmt->rowCount()>0){
  133. return true;
  134. }
  135. } else {
  136. return false;
  137. }
  138. }
  139. public function delete($where=''){
  140. $sql = "DELETE FROM {$table} ";
  141. //查询条件
  142. if(!empty($where)){
  143. $sql .= ' WHERE '.$where;
  144. }
  145. //创建PDO预处理对象
  146. $stmt = $this->pdo->prepare($sql);
  147. //执行删除操作
  148. if($stmt->execute()){
  149. if($stmt->rowCount()>0){
  150. return true;
  151. }
  152. } else {
  153. return false;
  154. }
  155. }
  156. public function count_num($where){
  157. $sql = 'SELECT count(*) as count_number FROM '.$this->table;
  158. //查询条件
  159. if(!empty($where)){
  160. $sql .= ' WHERE '.$where;
  161. }
  162. //创建PDO预处理对象
  163. $stmt = $this->pdo->prepare($sql);
  164. //执行查询操作
  165. if($stmt->execute()){
  166. if($stmt->rowCount()>0){
  167. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  168. $rows = $row['count_number'];
  169. return $rows;
  170. }
  171. } else {
  172. return false;
  173. }
  174. }
  175. }
  176. $db = new DB('mysql:host=localhost;dbname=anguoguo','root','root','movies');
  177. //连接pdo
  178. print_r($db);
  179. $result1 = $db->select('name','cate_id=2','mov_id DESC');
  180. print_r($result1);
  181. //echo是输出类型--Array,结果集用print_r输出
  182. echo '<hr/>';
  183. $result2 = $db->find('image,name','mov_id=5');
  184. print_r($result2);
  185. echo '<hr/>';
  186. $array3 =[
  187. 'mov_id' =>10,
  188. 'name' =>'插入',
  189. 'image'=>'10.jpg',
  190. 'detail'=>'能否插入成功',
  191. 'cate_id'=>'4'
  192. ];
  193. $result3 = $db->insert($array3);
  194. //插入是否有问题--当为数组时,不用加‘’,返回值插入成功--1
  195. print_r($result3);
  196. echo '<hr/>';
  197. $result4 = $db->update('[image=>10.jpg]','mov_id=5');
  198. print_r($result4);
  199. echo '<hr/>';
  200. $result5 = $db->delete('');
  201. print_r($result5);
  202. echo '<hr/>';
  203. $result6 = $db->count_num('mov_id');
  204. print_r($result6);
  205. echo '<hr/>';

另外

类名不区分大小写,函数名区分
构造方法是预处理,不是一定输出

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