獲取數據有返回數組形式或對象形式,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