获取数据有返回数组形式或对象形式,row_array()、result_array()是以数组形式返回;row()、result()是以对象形式返回。一样的,更新、删除、新增数据,也是能够传数组或对象形式进行操做的。php
方式一html
/** * 按sku获取一条数据 * @param $sku * @return mixed */ public function getOneRowBySku($sku) { $where = ['sku' => $sku, 'is_translated' => 1]; return $this->db->where($where)->get($this->tableName)->row_array(); }
方式二sql
//查物流商详情 public function getRowArray($code) { $sql="select id,name,code from ueb_cargo_company where code= '$code'"; $result = $this->db->query($sql); return $result->row_array(); }
//方式一 $sql = "INSERT INTO mytable (title, name) VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")"; $this->db->query($sql); if($this->db->affected_rows()){ echo '插入成功'; }else{ echo '插入失败'; } //方式二 $data = array( 'title' => 'My title', 'name' => 'My Name', 'date' => 'My date' ); $this->db->insert($this->tableName, $data); //批量插入 $data = array( array( 'title' => 'My title', 'name' => 'My Name', 'date' => 'My date' ), array( 'title' => 'Another title', 'name' => 'Another Name', 'date' => 'Another date' ) ); $this->db->insert_batch('mytable', $data);
//添加更新条件 $this->db->where('declaration_cn_name', $declaration_cn_name); //更新 $this->db->update($this->tableName, $updateData); // 批量更新 $data = [ [ 'title' => 'My title' , 'name' => 'My Name 2' , 'date' => 'My date 2' ], [ 'title' => 'Another title' , 'name' => 'Another Name 2' , 'date' => 'Another date 2' ] ]; //按$index字段条件对$data数据集中的数据进行批量更新 $this->db->update_batch($this->tableName, $data, $index='title'); //上面,第一个参数为要更新的表名,第二个参数为要更新的数据,是个二维数组,第三个参数是 WHERE 语句的键。
//删除一条数据 $this->db->delete('mytable', array('id' => $id)); //清空表数据 $this->db->empty_table('mytable'); // Produces: DELETE FROM mytable
$where = ['sku' => $sku, 'is_translated' => 1]; return $this->db->where($where)->get($this->tableName)->num_rows();
//调用有道翻译接口 $this->load->library('youdao_translate_api'); $content = $this->youdao_translate_api->translate($content);
主要仍是看官方文档,官方文档写的很清楚,总结算是提炼本身经常使用的东西,方便下次快速查到,提升效率api