1)$query = $this->db->get('sites'); //sites為表名2)$this->db->from('sites');$query = $this->db->get();(2)獲取數據表中特定的字段:select$this->db->select('url','name','clientid'); //'url','name','clientid'為數據表字段$query = $this->db->get('sites');(3)排序:order_by$this->db->order_by();
幫助你設置一個 ORDER BY 子句。第一個參數是你想要排序的字段名。第二個參數設置結果的順序,可用的選項包括asc(升序)或desc(降序), 或random(隨機)。
$this->db->order_by("title", "desc");// 生成: ORDER BY title DESC你也可以在第一個參數中傳遞你自己的字符串:
$this->db->order_by('title desc, name asc');// 生成: ORDER BY title DESC, name ASC或者,多次調用本函數就可以排序多個字段。
$this->db->order_by("title", "desc");$this->db->order_by("name", "asc"); // 生成: ORDER BY title DESC, name ASC例:$this->db->select('url','name','clientid');$this->db->order_by("name", "desc");$query = $this->db->get('sites');(4)限制返回數據記錄條數:limit$this->db->select('url','name','clientid');//'url','name','clientid'為列名$this->db->orderby("name", "desc");$this->db->limit(5); // 最初五條數據$query = $this->db->get('sites');注:$this->db->limit($pageSize,$start)$pageSize:每頁顯示條數$start:起始條數(5)條件:where1)‘=’的情況:$this->db->where('clientid', '1'); //clientid屬性 "1"為屬性值2)‘!=’的情況:$this->db->where('url !=', 'www.mysite.com');$this->db->where('id >', '3');3)多個where條件的情況:$this->db->where('url !=','www.mysite.com');$this->db->where('id >', '3');4)WHERE…OR的情況:或$this->db->where('url !=','www.mysite.com' );$this->db->orwhere('url !=','www.anothersite.com' );5)like:$this->db->like('title', 'match', 'before');// 生成: WHERE title LIKE '%match'$this->db->like('title', 'match', 'after');// 生成: WHERE title LIKE 'match%'$this->db->like('title', 'match', 'both');// 生成: WHERE title LIKE '%match%'(6)組合查詢:join$this->db->from('sites');$this->db->join('people', 'sites.peopleid = people.id');如:寫個完整的查詢$this->db->select('url','name','clientid','people.surname AS client');$this->db->where('clientid', '3');$this->db->limit(5);$this->db->from('sites');$this->db->join('people', 'sites.clientid = people.id','LEFT');$this->db->order_by("name", "desc");$this->db->group_by('clientid.id');$query = $this->db->get();(7)顯示查詢結果:在查詢語句后加上下面這句話:$query = $this->db->get();如果有多個結果,他們被保存在$row對象中,可以用一個 foreach 循環:foreach ($query->result() as $row){PRint $row->url;print $row->name;print $row->client;}如果我們只想要一個結果,它可以作為一個對象被返回, 或在這里當做一個$row數組if ($query->num_rows() > 0) // $query->num_rows() 返回查詢結果條數{$row = $query->row_array(); // 返回結果為 數組print $row['url'];print $row['name'];print $row['client'];}2、Insert 添加 方法一:先建個數組,把要insert的值放在數組里.如下:其中url/name/clientid/type均為數據表屬性值$data = array('url' => 'www.mynewclient.com','name' => 'BigCo Inc','clientid' => '33','type' => 'dynamic'); $this->db->insert('sites', $data);方法二:使用$this->db->set() 設置每一個值$this->db->set('url', 'www.mynewclinet.com');$this->db->set('name', 'BigCo Inc');$this->db->set('clientid', '33');$this->db->set('type', 'dynamic');$this->db->insert('sites');3、Update 更新 注:先定位要更新的記錄,再Update 方式一:使用數組形式$this->db->where('id', '1');$data = array('url' => 'www.mynewclient.com','name' => 'BigCo Inc','clientid' => '33','type' => 'dynamic');$this->db->update('sites', $data);方式二:使用$this->db->set() 設置每一個值$this->db->where('id', '1');$this->db->set('url', 'www.mynewclinet.com');$this->db->set('name', 'BigCo Inc');$this->db->set('clientid', '33');$this->db->set('type', 'dynamic');$this->db->update('sites');4、Delete 刪除 注:先定位要刪除的記錄,再 Delete$this->db->where('id', '2');$this->db->delete('sites');5、檢測數據庫是否執行成功 (1)$this->db->affected_rows(); (2)$new_id_number = $this->db->insert_id(); // 執行insert添加時,可以通過此函數返回添加之后記錄的ID
新聞熱點
疑難解答