今天,继续完善php新浪微博。优化代码,通过新的方法的引入是的代码更简介、逻辑性更强;然后使用模块化的代码实现增删改功能。
一、代码优化
之前的代码,通过一次性的写入sql来实现查询功能,但是存在不够灵活,尤其是where order等条件为空时,代码只有where关键字没有条件,sql语句不正确,代码无法实现,所以通过判断如果有where等条件的话,在初始化的语句中加入条件语句形成完整sql语句。具体如下:
实例
$sql = "SELECT {$this->field} FROM {$this->table}"; $this->where && $sql .= " WHERE {$this->where}"; $this->order && $sql .= " ORDER BY {$this->order}";
运行实例 »
点击 "运行实例" 按钮查看在线实例
还有,在新建的构建sql语句的方法可以添加变量,判断变量值来实现增删改查在一个方法下分别实现。
实例
private function _biuld_sql ($type, $data = '') { //查询sql if ($type == 'select') { $sql = "SELECT {$this->field} FROM {$this->table}"; $this->where && $sql .= " WHERE {$this->where}"; $this->order && $sql .= " ORDER BY {$this->order}"; } //更新sql if ($type == 'update') { $sql = $this->_biuld_update($data); } //插入sql if ($type == 'insert') { $sql = $this->_biuld_insert($data); } if ($type == 'delete') { $sql = $this->_biuld_delete(); } return $sql; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
二、增删改功能
如果把正删改查这些代码都放在一个方法里,代码就有点过长,可读性就差,所以把删改查功能在单独通过私有方法实现,然后在构建sql的方法中再去分别调用
- 三、总结
实例
<?php class Db { //初始化pdo private function init () { //设置dns $dns = 'mysql:dbname=test'; $username = 'root'; $psw = 'root'; //连接数据库 $this->pdo = new PDO($dns, $username, $psw); //显示错误提示 $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $this; } //查询表 public function table ($table) { $this->init(); $this->field = '*'; $this->order = ''; $this->where = ''; $this->table = $table; return $this; } //查询字段 public function field ($field) { $this->field = $field; return $this; } //查询条件 public function where ($where) { $this->where = $where; return $this; } //排序 public function order ($order) { $this->order = $order; return $this; } //limit public function limit () { } //单条结果 public function item () { //sql语句 $sql = $this->_biuld_sql('select').' LIMIT 1'; // exit($sql); //准备对象 $stmt = $this->pdo->prepare($sql); //执行 $stmt->execute(); //输出结果集 $item = $stmt->fetchAll(PDO::FETCH_ASSOC); return $item ? $item[0] : false; } //结果集 public function lists() { //sql语句 $sql = $this->_biuld_sql('select'); // exit($sql); //准备对象 $stmt = $this->pdo->prepare($sql); //执行 $stmt->execute(); //输出结果集 $item = $stmt->fetchAll(PDO::FETCH_ASSOC); return $item ? $item : false; } //更新操作 public function update ($data) { //sql语句 $sql = $this->_biuld_sql('update', $data); // exit($sql); $stmt = $this->pdo->prepare($sql); return $stmt->execute(); } //插入操作 public function insert ($data) { //sql语句 $sql = $this->_biuld_sql('insert', $data); $stmt = $this->pdo->prepare($sql); return $stmt->execute(); } //删除操作 public function delete () { //sql语句 $sql = $this->_biuld_sql('delete'); $stmt = $this->pdo->prepare($sql); return $stmt->execute(); } //构建查询sql private function _biuld_sql ($type, $data = '') { //查询sql if ($type == 'select') { $sql = "SELECT {$this->field} FROM {$this->table}"; $this->where && $sql .= " WHERE {$this->where}"; $this->order && $sql .= " ORDER BY {$this->order}"; } //更新sql if ($type == 'update') { $sql = $this->_biuld_update($data); } //插入sql if ($type == 'insert') { $sql = $this->_biuld_insert($data); } if ($type == 'delete') { $sql = $this->_biuld_delete(); } return $sql; } //构建update的sql语句方法 private function _biuld_update ($data) { $sql = "UPDATE {$this->table}"; //数据是数组的情况 if (is_array($data)) { $str = " SET "; foreach ($data as $key=>$value) { if (gettype($value) == 'string') { $str .= $key . "='" . $value ."',"; } else { $str .= $key . "=" . $value .","; } } $sql .= rtrim($str, ','); } else { $data && $sql .= " SET {$data}"; } $this->where && $sql .= " WHERE {$this->where}"; return $sql; } //构建insert的方法 private function _biuld_insert ($data) { $fields = $values = []; foreach ($data as $key=>$value) { $fields[] = $key; if (gettype($value) == 'string') { $values[] = "'" . $value . "'"; } else { $values[] = $value; } } $str_fields = implode($fields, ','); $str_values = implode($values, ','); $sql = "INSERT INTO {$this->table} ({$str_fields}) VALUES ($str_values)"; return $sql; } private function _biuld_delete () { $sql = "DELETE FROM {$this->table}"; if (!$this->where) { return $sql; } $where = " WHERE "; if (is_array($this->where)) { foreach ($this->where as $key=>$value) { gettype($value) == 'string' && $value = '"' .$value .'"'; $where .= $key . '=' .$value .' and '; } $sql .= rtrim($where, ' and '); } else { $sql .= " WHERE {$this->where}"; } return $sql; } }
运行实例 »点击 "运行实例" 按钮查看在线实例
要想代码可维护性强,就用把代码模块化,一个模块实现一个功能,大的功能由几个小模块组合实现;
sql增删改查语句在复习
php基础知识,尤其是字符串以及其他数据类型转换的函数要学习