国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

CI(5) SQL常用操作

2019-11-06 08:19:54
字體:
來源:轉載
供稿:網友
注:執行SQL語句時,首先要確保已經與數據庫鏈接:$this->db->database();1、Select查詢    (1)獲取數據表所有記錄(取回全部數據,相當于 select * ):
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)條件:where
1)‘=’的情況:
$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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莱西市| 来凤县| 台东市| 克拉玛依市| 奉化市| 农安县| 武冈市| 东宁县| 东丽区| 贡山| 韶山市| 彩票| 颍上县| 崇信县| 屯留县| 宿州市| 吴江市| 金华市| 东明县| 丹江口市| 柘荣县| 博兴县| 周口市| 固始县| 宁化县| 丰宁| 许昌市| 苏尼特左旗| 福安市| 深圳市| 深州市| 安乡县| 台中县| 仙游县| 明溪县| 建宁县| 达日县| 彭山县| 郯城县| 枣庄市| 伊吾县|