CI中的數據庫操作


CI中的數據庫操作

 

在system/application/config 文件夾和里面的config文件里已經配置了參數

$active_group = "default";
$db['default']['hostname'] = "";  hostname: 你的數據庫的位置, 舉例來說, 'localhost' 或 IP 地址 
$db['default']['username'] = "";  username和password: 使用者名稱和密碼必須有充分的權限,允許你的網站存取數據庫中的數據。
$db['default']['password'] = ""; 
$db['default']['database'] = "";  database: 你的數據庫的名字, 舉例來說, 'websits' 
$db['default']['dbdriver'] = "";  dbdriver: 你正在使用的數據庫的類型 - CI可受的有選項有MySQL、MySQLi、 Postgre SQL、ODBC和MS SQL

CI中第一次連接數據庫,在控制器或模型的構造函數里輸入以下語句
$this->load->database();
就不需要重復連接, 在那個控制器或模型就可以做任意多次的查詢。


查詢操作(等同select)
方法一:
$query = $this->db->get('sites'); //sites為表名
這是一個“select *”查詢,目標是site表。換句話說,它取回所有的行
也可用下面這種方式寫:
$this->db->from('sites');
$query = $this->db->get();
如果想要得到特定的列,而不是全部列,這樣做:
$this->db->select('url','name','clientid');//'url','name','clientid'為列名
$query = $this->db->get('sites');
如果排序:
$this->db->select('url','name','clientid');//'url','name','clientid'為列名
$this->db->orderby("name", "desc");
$query = $this->db->get('sites');
如果想要限制返回的行數,比如想要最初五個結果
$this->db->select('url','name','clientid');//'url','name','clientid'為列名
$this->db->orderby("name", "desc");
$this->db->limit(5);
$query = $this->db->get('sites');
寫where語句
==的情況
$this->db->where('clientid', '1');  //clientid屬性  "1"為屬性值
!=的情況
$this->db->where('url !=', 'www.mysite.com');
$this->db->where('id >', '3');
where后幾個條件的可以寫幾個where 如
$this->db->where('url !=','www.mysite.com');
$this->db->where('id >', '3');
WHERE…OR的情況
$this->db->where('url !=','www.mysite.com' );
$this->db->orwhere('url !=','www.anothersite.com' );
連接表
$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');
$this->db->orderby("name", "desc");
$query = $this->db->get();
方法二:
$this->db->query("SELECT id, name, url FROM sites WHERE 'type' = 'dynamic'");
可以像下面的語句一樣寫查詢放條件
$condition = "client ='3' AND (type ='dynamic' OR type='static')";
$this->db->where($condition);
注意:雙引號是定義變量的.不要混淆單引號和雙引號.


顯示查詢結果
在查詢語句后加上下面這句話
$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)
{
   $row = $query->row_array();
 
   print $row['url'];
   print $row['name'];
   print $row['client'];
}


增加數據(等同insert)
方法一:先建個數組,把要insert的值放在數組里.如下:其中url/name/clientid/type均為數據表屬性值
$data = array(
                'url' => 'www.mynewclient.com',
                'name' => 'BigCo Inc',
                'clientid' => '33',
                'type' => 'dynamic'
            );
然后使用$this->db->insert('sites', $data); 把數據增加到sites表中.
方法二:使用$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');


更新(等同update)
先定位要更新的記錄,再update
$this->db->where('id', '1');
$this->db->update('sites', $data);
$this->db->set()方式也可以,和新增數據應該是一樣的.

 

CI 提供幾個函數檢查數據庫是否成功執行了相關操作。 最有用的:
$this->db->affected_rows();
在執行insert或update后應該返回 '1'-但是如果我正在update一批記錄的話,可能返回更大的一個整數。


如果我正在insert一筆新的記錄, 在實際產生它之前,我們並不知道ID具體的值。如果我需要引用新的記錄的ID, 使用下列語句:
$new_id_number = $this->db->insert_id();


刪除(等同delete)
$this->db->where('id', '2');
$this->db->delete('sites');


 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM