[关闭]
@a5635268 2018-02-01T15:58:06.000000Z 字数 3965 阅读 1072

通过一个简单的数据库操作类了解PHP链式操作的实现

PHP


  1. class Model{
  2. public $table; //操作的表;
  3. private $opt; //查询的参数;
  4. private $pri; //表的主键;
  5. private $lastSql; //最后一条sql语句;
  6. private $error;
  7. public function __construct($table = ''){
  8. $this->table = $table;
  9. $db = @mysql_connect('127.0.0.1' , 'root' , 'admin'); //这里应该通过配置读取进来
  10. if (!$db)
  11. exit('抱歉!数据库链接失败!');
  12. mysql_select_db('tpplh') or exit('对不起,没有这个数据库'); //数据库的选择
  13. mysql_query("SET NAMES utf8"); //设定数据库字符集;
  14. $this->getPrimary($table); //获取主键;
  15. $this->opt(); //初始化查询参数;
  16. }
  17. public function getLastSql(){
  18. return $this->lastSql;
  19. }
  20. public function getError(){
  21. return $this->error();
  22. }
  23. #获取表的主键
  24. private function getPrimary($table){
  25. $this->lastSql = ' DESC ' . $table;
  26. $result = $this->query($this->lastSql);
  27. foreach ($result as $v) {
  28. if ($v['Key'] == 'PRI') {
  29. $this->pri = $v['Field'];
  30. return;
  31. }
  32. }
  33. }
  34. #初始化查询参数
  35. private function opt(){
  36. $this->opt = array('table' => $this->table , 'pri' => $this->pri , 'where' => '' , 'orderby' => '' , 'having' => '' , 'orderway' => '' , 'group' => '' , 'limit' => '' , 'fields' => '*');
  37. }
  38. #临时表切换,不用另外在实例化类了;
  39. public function table($table){
  40. $this->opt['table'] = $table;
  41. return $this; //返回切换后的对象;
  42. }
  43. public function save($data = null){
  44. return $this->update($data);
  45. }
  46. #update更改
  47. public function update($data = null){
  48. if (is_null($data))
  49. $data = $_POST;
  50. if (!is_array($data))
  51. return false;
  52. if (isset($data[$this->pri])) { //如果里面有主键;就让where条件临时变为主键
  53. $this->opt['where'] = ' WHERE ' . $this->pri . '=' . $data[$this->pri];
  54. }
  55. if (!$this->opt['where']) {
  56. echo '增删改操作,必须要带where条件!';
  57. exit;
  58. }
  59. $set = ''; //set字符串容器;
  60. foreach ($data as $n => $v) {
  61. $set .= $n . "='" . $v . "',";
  62. }
  63. $set = rtrim($set , ','); //去掉后面多余的逗号;
  64. $this->lastSql = "UPDATE " . $this->opt['table'] . ' SET ' . $set . $this->opt['where'];
  65. return $this->exe($this->lastSql);
  66. }
  67. #delete删除:delete from 表名 where 条件;
  68. public function delete(){
  69. $where = $this->opt['where'];
  70. if (!$where) {
  71. echo 'delete必须设置条件';
  72. exit;
  73. }
  74. $this->lastSql = "DELETE FROM {$this->opt['table']} $where";
  75. return $this->exe($this->lastSql);
  76. }
  77. #insert条件
  78. public function insert($data = null){
  79. if (is_null($data))
  80. $data = $_POST;
  81. if (!is_array($data))
  82. return false;
  83. $field = $value = "";
  84. $field = implode("," , array_keys($data));
  85. foreach ($data as $name => $v) {
  86. $value .= "'" . addslashes($v) . "',";
  87. }
  88. $value = substr($value , 0 , -1);
  89. $this->lastSql = "INSERT INTO " . $this->opt['table'] . "($field) VALUES($value)";
  90. return $this->exe($this->lastSql);
  91. }
  92. public function add($data = null){
  93. return $this->insert($data);
  94. }
  95. #limit 语句:
  96. public function limit($arg){
  97. $this->opt['limit'] = " LIMIT $arg";
  98. return $this;
  99. }
  100. #order 语句:
  101. public function order($arg){
  102. $this->opt['orderby'] = " ORDER BY $arg";
  103. return $this;
  104. }
  105. #orderway 语句:
  106. public function orderway($arg){
  107. $this->opt['orderway'] = " $arg";
  108. return $this;
  109. }
  110. #group 语句:
  111. public function group($arg){
  112. $this->opt['group'] = " GROUP BY $arg";
  113. return $this;
  114. }
  115. #where 条件
  116. public function where($arg){
  117. $this->opt['where'] = " WHERE $arg";
  118. return $this;
  119. }
  120. #having 语句
  121. public function having($arg){
  122. $this->opt['where'] = " HAVING $arg";
  123. return $this;
  124. }
  125. #fields 语句
  126. public function field($arg){
  127. $this->opt['fields'] = " $arg";
  128. return $this;
  129. }
  130. #count 语句 统计数量
  131. public function count(){
  132. $this->lastSql = "select count(*) as t from " . $this->opt['table'] . $this->opt['where'];
  133. $count = $this->query($this->lastSql);
  134. return $count[0]['t'];
  135. }
  136. #select 语句
  137. public function select(){
  138. $this->lastSql = "SELECT {$this->opt['fields']} FROM {$this->opt['table']}{$this->opt['where']}{$this->opt['orderby']}{$this->opt['orderway']}{$this->opt['group']}{$this->opt['having']}{$this->opt['limit']}";
  139. //echo $this -> lastSql;die;
  140. $this->opt(); //查询完执行opt方法重新初始化参数;
  141. return $this->query($this->lastSql);
  142. }
  143. #find 通过主键来查询
  144. public function find($arg = null){
  145. if ($this->pri and $arg) {
  146. $this->opt['where'] = " WHERE {$this->pri}=$arg";
  147. } else {
  148. $this->opt['limit'] = " LIMIT 1";
  149. }
  150. return $this->select();
  151. }
  152. #发送增,删,改sql,成功返回最后一条的id或受影响条数;
  153. public function exe($sql){
  154. $this->lastSql = $sql;
  155. if (mysql_query($sql)) {
  156. $result = mysql_insert_id() ? mysql_insert_id() : mysql_affected_rows(); //如果该表有一个自增的id,就返回自增的id,如果没有就返回受影响的条数;
  157. return $result;
  158. }
  159. }
  160. #发送查询的sql语句,返回查询结果数组;
  161. public function query($sql){
  162. $this->lastSql = $sql;
  163. $result = mysql_query($sql);
  164. if (!$result)
  165. return $this->error();
  166. $rows = array(); //用于存储查询的数据;
  167. while ($row = mysql_fetch_assoc($result)) {
  168. $rows[] = $row;
  169. }
  170. return $rows;
  171. }
  172. private function error(){
  173. return $this->error = mysql_error(); //输出mysql产生的错误信息;
  174. }
  175. }
  176. ?>

总结如下:

  1. 链式部分就是对类中属性的进行更改,属性可以是数组
  2. 在链式部分最后返回的都是$this
  3. 在最后输出方法中获取链式更改掉的属性进行操作
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注