亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

direktori cari
歡迎 目錄 快速參考圖 基本信息 服務(wù)器要求 許可協(xié)議 變更記錄 關(guān)于CodeIgniter 安裝 下載 CodeIgniter 安裝指導(dǎo) 從老版本升級 疑難解答 介紹 開始 CodeIgniter 是什么? CodeIgniter 速記表 支持特性 應(yīng)用程序流程圖 模型-視圖-控制器 架構(gòu)目標(biāo) 教程 內(nèi)容提要 加載靜態(tài)內(nèi)容 創(chuàng)建新聞條目 讀取新聞條目 結(jié)束語 常規(guī)主題 CodeIgniter URL 控制器 保留字 視圖 模型 輔助函數(shù) 使用 CodeIgniter 類庫 創(chuàng)建你自己的類庫 使用 CodeIgniter 適配器 創(chuàng)建適配器 創(chuàng)建核心系統(tǒng)類 鉤子 - 擴(kuò)展框架的核心 自動裝載資源 公共函數(shù) URI 路由 錯誤處理 緩存 調(diào)試應(yīng)用程序 以CLI方式運(yùn)行 管理應(yīng)用程序 處理多環(huán)境 PHP替代語法 安全 開發(fā)規(guī)范 類庫參考 基準(zhǔn)測試類 日歷類 購物車類 配置類 Email 類 加密類 文件上傳類 表單驗證詳解 FTP 類 圖像處理類 輸入類 Javascript 類 語言類 裝載類 遷移類 輸出類 分頁類 模板解析器類 安全類 Session 類 HTML 表格類 引用通告類 排版類 單元測試類 URI 類 User-Agent 類 表單驗證 XML-RPC 和 XML-RPC 服務(wù)器 Zip 編碼類 緩存適配器 適配器參考 適配器 數(shù)據(jù)庫類 Active Record 類 數(shù)據(jù)庫緩存類 自定義函數(shù)調(diào)用 數(shù)據(jù)庫配置 連接你的數(shù)據(jù)庫 數(shù)據(jù)庫快速入門例子代碼 字段數(shù)據(jù) 數(shù)據(jù)庫維護(hù)類 查詢輔助函數(shù) 數(shù)據(jù)庫類 查詢 生成查詢記錄集 表數(shù)據(jù) 事務(wù) 數(shù)據(jù)庫工具類 JavaScript類 輔助函數(shù)參考 數(shù)組輔助函數(shù) CAPTCHA 輔助函數(shù) Cookie Helper 日期輔助函數(shù) 目錄輔助函數(shù) 下載輔助函數(shù) Email 輔助函數(shù) 文件輔助函數(shù) 表單輔助函數(shù) HTML輔助函數(shù) Inflector 輔助函數(shù) 語言輔助函數(shù) 數(shù)字輔助函數(shù) 路徑輔助函數(shù) 安全輔助函數(shù) 表情輔助函數(shù) 字符串輔助函數(shù) 文本輔助函數(shù) 排版輔助函數(shù) URL 輔助函數(shù) XML 輔助函數(shù)
watak

CodeIgniter 用戶指南 版本 2.1.0

編輯文檔、查看近期更改請 登錄 或 注冊  找回密碼
查看原文

Active Record 類

CodeIgniter 使用的是修改過的 Active Record 數(shù)據(jù)庫模式。 這種模式是以較少的程序代碼來實(shí)現(xiàn)信息在數(shù)據(jù)庫中的獲取,插入,更改。 有時只用一兩行的代碼就能完成對數(shù)據(jù)庫的操作。 CodeIgniter 不需要每一個數(shù)據(jù)庫表擁有自己的類。它提供了一個更簡單的接口。

不只是簡單,使用 Active Record 的一個主要的優(yōu)點(diǎn)是允許你創(chuàng)建獨(dú)立的數(shù)據(jù)庫應(yīng)用程序,因為查詢語法是由數(shù)據(jù)庫的適配器來產(chǎn)生的。它可以進(jìn)行更安全的查詢,因為系統(tǒng)會自動的對所有的輸入值進(jìn)行轉(zhuǎn)義。

注意: 你如果想要寫自己的查詢表達(dá)式,可以在數(shù)據(jù)庫配置文件中禁止這個類。同時相比 Active Record,使用核心數(shù)據(jù)庫類庫和適配器將會使用更少資源。

  • 選擇數(shù)據(jù)
  • 插入數(shù)據(jù)
  • 更新數(shù)據(jù)
  • 刪除數(shù)據(jù)
  • 鏈?zhǔn)椒椒?/li>
  • Active Record 緩存
?

選擇數(shù)據(jù)

下面的函數(shù)幫助你構(gòu)建 SQL SELECT語句。

備注:如果你正在使用 PHP5,你可以在復(fù)雜情況下使用鏈?zhǔn)秸Z法。本頁面底部有具體描述。

$this->db->get();

運(yùn)行選擇查詢語句并且返回結(jié)果集??梢垣@取一個表的全部數(shù)據(jù)。

$query = $this->db->get('mytable');

// Produces: SELECT * FROM mytable

第二和第三個參數(shù)允許你設(shè)置一個結(jié)果集每頁紀(jì)錄數(shù)(limit)和結(jié)果集的偏移(offset)

$query = $this->db->get('mytable', 10, 20);

// Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)

注意:第二參數(shù)是每頁紀(jì)錄數(shù),第三個參數(shù)是偏移

你會注意到上面的函數(shù)由一個變量$query執(zhí)行,這個$query可以用來顯示結(jié)果集。

$query = $this->db->get('mytable');

foreach ($query->result() as $row)
{
????echo $row->title;
}

請訪問查詢結(jié)果頁查看詳細(xì)的生成結(jié)果的方法。

$this->db->get_where();

跟上面的函數(shù)一樣,只是它允許你在函數(shù)的第二個參數(shù)那里添加一個 where 從句,從而不用使用 db->where() 這個函數(shù):

$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);

請閱讀下面的 where 函數(shù)了解更多。

注意: get_where() 在以前的版本中寫作 getwhere(),這是已經(jīng)過時的用法,現(xiàn)已從代碼中移除 getwhere()。

$this->db->select();

允許你在SQL查詢中寫 SELECT 部分:

$this->db->select('title, content, date');

$query = $this->db->get('mytable');

// Produces: SELECT title, content, date FROM mytable

注意: 如果你要查詢表中的所有行,你可以不用寫這個函數(shù)。省略后,CodeIgniter 會認(rèn)為你要查詢 全部行(SELECT *)。

$this->db->select() 可接受一個可選的第二個參數(shù)。如果你把它設(shè)為FALSE, CodeIgniter 將不會使用反引號保護(hù)你的字段或者表名 。這在進(jìn)行復(fù)合查詢時很有用。

$this->db->select("('SELECT SUM(payments.amount) FROM payments WHERE payments.invoice_id=4') AS amount_paid", FALSE);
$query = $this->db->get('mytable');

$this->db->select_max();

為你的查詢編寫一個 "SELECT MAX(field)"。你可以選擇性的給出第二個參數(shù),以便重命名結(jié)果字段名。

$this->db->select_max('age');
$query = $this->db->get('members');
// Produces: SELECT MAX(age) as age FROM members

$this->db->select_max('age', 'member_age');
$query = $this->db->get('members');
// Produces: SELECT MAX(age) as member_age FROM members

$this->db->select_min();

為你的查詢編寫一個 "SELECT MIN(field)" 。與 select_max() 相似, 你可以選擇性地給出第二個參數(shù),用來給結(jié)果字段重命名。

$this->db->select_min('age');
$query = $this->db->get('members');
// 生成: SELECT MIN(age) as age FROM members

$this->db->select_avg();

為你的查詢編寫一個 "SELECT AVG(field)" 。與 select_max() 相似, 你可以選擇性地給出第二個參數(shù),用來給結(jié)果字段重命名。

$this->db->select_avg('age');
$query = $this->db->get('members');
// 生成: SELECT AVG(age) as age FROM members

$this->db->select_sum();

為你的查詢編寫一個 "SELECT SUM(field)" 。與 select_max() 相似, 你可以選擇性地給出第二個參數(shù),用來給結(jié)果字段重命名。

$this->db->select_sum('age');
$query = $this->db->get('members');
// 生成: SELECT SUM(age) as age FROM members

$this->db->from();

允許你編寫查詢中的FROM部分:

$this->db->select('title, content, date');
$this->db->from('mytable');

$query = $this->db->get();

// 生成: SELECT title, content, date FROM mytable

說明: 正如前面所說,查詢中的FROM部分可以在 $this->db->get() 函數(shù)中指定,所以你可以根據(jù)自己的喜好來選擇使用哪個方法。

$this->db->join();

允許你編寫查詢中的JOIN部分:

$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');

$query = $this->db->get();

// 生成:
// SELECT * FROM blogs
// JOIN comments ON comments.id = blogs.id

如果你想要在查詢中使用多個連接,可以多次調(diào)用本函數(shù)。

如果你需要指定 JOIN 的類型,你可以通過本函數(shù)的第三個參數(shù)來指定??蛇x項包括:left, right, outer, inner, left outer, 以及 right outer.

$this->db->join('comments', 'comments.id = blogs.id', 'left');

// 生成: LEFT JOIN comments ON comments.id = blogs.id

$this->db->where();

本函數(shù)允許你使用四種方法中的一種來設(shè)置 WHERE 子句:

說明: 傳遞給本函數(shù)的所有值都會被自動轉(zhuǎn)義,以便生成安全的查詢。

  1. 簡單的 key/value 方法: $this->db->where('name', $name);

    // 生成: WHERE name = 'Joe'

    請注意等號已經(jīng)為你添加。

    如果你多次調(diào)用本函數(shù),那么這些條件會被 AND 連接起來:

    $this->db->where('name', $name);
    $this->db->where('title', $title);
    $this->db->where('status', $status);

    // WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
  2. 自定義 key/value 方法:

    你可以在第一個參數(shù)中包含一個運(yùn)算符,以便控制比較:

    $this->db->where('name !=', $name);
    $this->db->where('id
    // 生成: WHERE name != 'Joe' AND id
  3. 關(guān)聯(lián)數(shù)組方法: $array = array('name' => $name, 'title' => $title, 'status' => $status);

    $this->db->where($array);

    // 生成: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'

    使用這個方法時你也可以包含運(yùn)算符:

    $array = array('name !=' => $name, 'id $id, 'date >' => $date);

    $this->db->where($array);
  4. 自定義字符串:

    你可以手動的編寫子句:

    $where = "name='Joe' AND status='boss' OR status='active'";

    $this->db->where($where);

$this->db->where() 接受可選的第三個參數(shù)。如果你將它設(shè)置為 FALSE, CodeIgniter 將不會為你那些包含反勾號的字段名或表名提供保護(hù)。

$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);

$this->db->or_where();

本函數(shù)與上面的那個幾乎完全相同,唯一的區(qū)別是本函數(shù)生成的子句是用 OR 來連接的:

$this->db->where('name !=', $name);
$this->db->or_where('id >', $id);

// 生成: WHERE name != 'Joe' OR id > 50

說明: or_where() 以前被叫作 orwhere(), 后者已經(jīng)過時,現(xiàn)已從代碼中移除 orwhere()。

$this->db->where_in();

生成一段 WHERE field IN ('item', 'item') 查詢語句,如果合適的話,用 AND 連接起來。

$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);
// 生成: WHERE username IN ('Frank', 'Todd', 'James')

$this->db->or_where_in();

生成一段 WHERE field IN ('item', 'item') 查詢語句,如果合適的話,用 OR 連接起來。

$names = array('Frank', 'Todd', 'James');
$this->db->or_where_in('username', $names);
// 生成: OR username IN ('Frank', 'Todd', 'James')

$this->db->where_not_in();

生成一段 WHERE field NOT IN ('item', 'item') 查詢語句,如果合適的話,用 AND 連接起來。

$names = array('Frank', 'Todd', 'James');
$this->db->where_not_in('username', $names);
// 生成: WHERE username NOT IN ('Frank', 'Todd', 'James')

$this->db->or_where_not_in();

生成一段 WHERE field NOT IN ('item', 'item') 查詢語句,如果合適的話,用 OR 連接起來。

$names = array('Frank', 'Todd', 'James');
$this->db->or_where_not_in('username', $names);
// 生成: OR username NOT IN ('Frank', 'Todd', 'James')

$this->db->like();

本函數(shù)允許你生成 LIKE 子句,在做查詢時非常有用。

說明: 傳遞給本函數(shù)的所有值都會被自動轉(zhuǎn)義。

  1. 簡單 key/value 方式: $this->db->like('title', 'match');

    // 生成: WHERE title LIKE '%match%'

    如果你多次調(diào)用本函數(shù),那么這些條件將由 AND 連接起來:

    $this->db->like('title', 'match');
    $this->db->like('body', 'match');

    // WHERE title LIKE '%match%' AND body LIKE '%match%'
    如果你想要控制通配符(%)所出現(xiàn)的位置,你可以使用可選的第三個參數(shù)。可用的選項是 'before', 'after' 以及 'both' (這是默認(rèn)值)。 $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%'
  2. If you do not want to use the wildcard (%) you can pass to the optional third argument the option 'none'. $this->db->like('title', 'match', 'none');
    // Produces: WHERE title LIKE 'match'
  3. 關(guān)聯(lián)數(shù)組方式: $array = array('title' => $match, 'page1' => $match, 'page2' => $match);

    $this->db->like($array);

    // WHERE title LIKE '%match%' AND page1 LIKE '%match%' AND page2 LIKE '%match%'

$this->db->or_like();

本函數(shù)與上面那個函數(shù)幾乎完全相同,唯一的區(qū)別是多個實(shí)例之間是用 OR 連接起來的:

$this->db->like('title', 'match');
$this->db->or_like('body', $match);

// WHERE title LIKE '%match%' OR body LIKE '%match%'

說明: or_like() 曾經(jīng)被稱為 orlike(), 后者已經(jīng)過時,現(xiàn)已從代碼中移除 orlike()。

$this->db->not_like();

本函數(shù)與 like() 幾乎完全相同,唯一的區(qū)別是它生成 NOT LIKE 語句:

$this->db->not_like('title', 'match');

// WHERE title NOT LIKE '%match%

$this->db->or_not_like();

本函數(shù)與 not_like() 幾乎完全相同,唯一的區(qū)別是多個實(shí)例之間是用 OR 連接起來的:

$this->db->like('title', 'match');
$this->db->or_not_like('body', 'match');

// WHERE title LIKE '%match%' OR body NOT LIKE '%match%'

$this->db->group_by();

允許你編寫查詢語句中的 GROUP BY 部分:

$this->db->group_by("title");

// 生成: GROUP BY title

你也可以把多個值作為數(shù)組傳遞過去:

$this->db->group_by(array("title", "date"));

// 生成: GROUP BY title, date

說明: group_by() 曾經(jīng)被稱為 groupby(), 后者已經(jīng)過時,現(xiàn)已從代碼中移除 groupby()。

$this->db->distinct();

為查詢語句添加 "DISTINCT" 關(guān)鍵字:

$this->db->distinct();
$this->db->get('table');

// 生成: SELECT DISTINCT * FROM table

$this->db->having();

允許你為你的查詢語句編寫 HAVING 部分。有兩種語法形式,一個或兩個參數(shù)都可以:

$this->db->having('user_id = 45');
// 生成: HAVING user_id = 45

$this->db->having('user_id', 45);
// 生成: HAVING user_id = 45

你也可以把多個值通過數(shù)組傳遞過去:

$this->db->having(array('title =' => 'My Title', 'id <' => $id));

// 生成: HAVING title = 'My Title' AND id

如果你正在使用一個由CodeIgniter進(jìn)行轉(zhuǎn)義保護(hù)的數(shù)據(jù)庫,為了避免內(nèi)容轉(zhuǎn)義,你可以傳遞可選的第三個參數(shù),并將其設(shè)置為FALSE。

$this->db->having('user_id', 45);
// 生成: HAVING `user_id` = 45 (在諸如MySQL等數(shù)據(jù)庫中)
$this->db->having('user_id', 45, FALSE);
// 生成: HAVING user_id = 45

$this->db->or_having();

與 having() 函數(shù)幾乎完全一樣,唯一的區(qū)別是多個子句之間是用 "OR" 分隔的。

$this->db->order_by();

幫助你設(shè)置一個 ORDER BY 子句。第一個參數(shù)是你想要排序的字段名。第二個參數(shù)設(shè)置結(jié)果的順序,可用的選項包括 asc (升序)或 desc(降序), 或 random(隨機(jī))。

$this->db->order_by("title", "desc");

// 生成: ORDER BY title DESC

你也可以在第一個參數(shù)中傳遞你自己的字符串:

$this->db->order_by('title desc, name asc');

// 生成: ORDER BY title DESC, name ASC

或者,多次調(diào)用本函數(shù)就可以排序多個字段。

$this->db->order_by("title", "desc");
$this->db->order_by("name", "asc");

// 生成: ORDER BY title DESC, name ASC

說明: order_by() 曾經(jīng)被稱為 orderby(), 后者已經(jīng)過時,現(xiàn)已從代碼中移除 orderby()。

說明: 目前 Oracle 和 MSSQL 的驅(qū)動還不支持隨機(jī)排序,將被默認(rèn)設(shè)置為 'ASC'(升序)。

$this->db->limit();

限制查詢所返回的結(jié)果數(shù)量:

$this->db->limit(10);

// 生成: LIMIT 10

第二個參數(shù)設(shè)置的是結(jié)果偏移量。

$this->db->limit(10, 20);

// 生成: LIMIT 20, 10 (僅限MySQL中。其它數(shù)據(jù)庫有稍微不同的語法)

$this->db->count_all_results();

允許你獲得某個特定的Active Record查詢所返回的結(jié)果數(shù)量??梢允褂肁ctive Record限制函數(shù),例如 where(), or_where(), like(), or_like() 等等。范例:

echo $this->db->count_all_results('my_table');
// 生成一個整數(shù),例如 25

$this->db->like('title', 'match');
$this->db->from('my_table');
echo $this->db->count_all_results();
// 生成一個整數(shù),例如 17
?

插入數(shù)據(jù)

$this->db->insert();

生成一條基于你所提供的數(shù)據(jù)的SQL插入字符串并執(zhí)行查詢。你可以向函數(shù)傳遞 數(shù)組 或一個 對象。下面是一個使用數(shù)組的例子:

$data = array(
???????????????'title' => 'My title' ,
???????????????'name' => 'My Name' ,
???????????????'date' => 'My date'
????????????);

$this->db->insert('mytable', $data);

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')

第一個參數(shù)包含表名,第二個是一個包含數(shù)據(jù)的關(guān)聯(lián)數(shù)組。

下面是一個使用對象的例子:



$object = new Myclass;

$this->db->insert('mytable', $object);

// Produces: INSERT INTO mytable (title, content, date) VALUES ('My Title', 'My Content', 'My Date')

第一個參數(shù)包含表名,第二個是一個對象。

注意: 所有的值已經(jīng)被自動轉(zhuǎn)換為安全查詢。

$this->db->insert_batch();

生成一條基于你所提供的數(shù)據(jù)的SQL插入字符串并執(zhí)行查詢。你可以向函數(shù)傳遞 數(shù)組 或一個 對象。下面是一個使用數(shù)組的例子:

$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);

//生成: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')

第一個參數(shù)包含表名,第二個是一個包含數(shù)據(jù)的關(guān)聯(lián)數(shù)組。

注意: 所有的值已經(jīng)被自動轉(zhuǎn)換為安全查詢。

$this->db->set();

本函數(shù)使您能夠設(shè)置inserts(插入)或updates(更新)值。

它可以用來代替那種直接傳遞數(shù)組給插入和更新函數(shù)的方式:

$this->db->set('name', $name);
$this->db->insert('mytable');

// 生成: INSERT INTO mytable (name) VALUES ('{$name}')

如果你多次調(diào)用本函數(shù),它們會被合理地組織起來,這取決于你執(zhí)行的是插入操作還是更新操作:

$this->db->set('name', $name);
$this->db->set('title', $title);
$this->db->set('status', $status);
$this->db->insert('mytable');

set() 也接受可選的第三個參數(shù)($escape),如果此參數(shù)被設(shè)置為 FALSE,就可以阻止數(shù)據(jù)被轉(zhuǎn)義。為了說明這種差異,這里將對 包含轉(zhuǎn)義參數(shù) 和 不包含轉(zhuǎn)義參數(shù) 這兩種情況的 set() 函數(shù)做一個說明。

$this->db->set('field', 'field+1', FALSE);
$this->db->insert('mytable');
// 得到 INSERT INTO mytable (field) VALUES (field+1)

$this->db->set('field', 'field+1');
$this->db->insert('mytable');
// 得到 INSERT INTO mytable (field) VALUES ('field+1')

你也可以將一個關(guān)聯(lián)數(shù)組傳遞給本函數(shù):

$array = array('name' => $name, 'title' => $title, 'status' => $status);

$this->db->set($array);
$this->db->insert('mytable');

或者一個對象也可以:



$object = new Myclass;

$this->db->set($object);
$this->db->insert('mytable');
?

更新數(shù)據(jù)

$this->db->update();

根據(jù)你提供的數(shù)據(jù)生成并執(zhí)行一條update(更新)語句。你可以將一個數(shù)組或者對象傳遞給本函數(shù)。這里是一個使用數(shù)組的例子:

$data = array(
???????????????'title' => $title,
???????????????'name' => $name,
???????????????'date' => $date
????????????);

$this->db->where('id', $id);
$this->db->update('mytable', $data);

// 生成:
// UPDATE mytable
// SET title = '{$title}', name = '{$name}', date = '{$date}'
// WHERE id = $id

或者你也可以傳遞一個對象:



$object = new Myclass;

$this->db->where('id', $id);
$this->db->update('mytable', $object);

// 生成:
// UPDATE mytable
// SET title = '{$title}', name = '{$name}', date = '{$date}'
// WHERE id = $id

說明: 所有值都會被自動轉(zhuǎn)義,以便生成安全的查詢。

你會注意到 $this->db->where() 函數(shù)的用法,它允許你設(shè)置 WHERE 子句。你可以有選擇性地將這一信息直接以字符串的形式傳遞給 update 函數(shù):

$this->db->update('mytable', $data, "id = 4");

或者是一個數(shù)組:

$this->db->update('mytable', $data, array('id' => $id));

在進(jìn)行更新時,你還可以使用上面所描述的 $this->db->set() 函數(shù)。

$this->db->update_batch();

Generates an update string based on the data you supply, and runs the query. You can either pass an array or an object to the function. Here is an example using an array:

$data = array(
???array(
??????'title' => 'My title' ,
??????'name' => 'My Name 2' ,
??????'date' => 'My date 2'
???),
???array(
??????'title' => 'Another title' ,
??????'name' => 'Another Name 2' ,
??????'date' => 'Another date 2'
???)
);

$this->db->update_batch('mytable', $data, 'title');

// Produces:
// UPDATE `mytable` SET `name` = CASE
// WHEN `title` = 'My title' THEN 'My Name 2'
// WHEN `title` = 'Another title' THEN 'Another Name 2'
// ELSE `name` END,
// `date` = CASE
// WHEN `title` = 'My title' THEN 'My date 2'
// WHEN `title` = 'Another title' THEN 'Another date 2'
// ELSE `date` END
// WHERE `title` IN ('My title','Another title')

參數(shù)1:表名 參數(shù)2:如上所示的二維數(shù)組 參數(shù)3:鍵名.

提示: 所有的值都會自動進(jìn)行安全性過濾.

?

刪除數(shù)據(jù)

$this->db->delete();

生成并執(zhí)行一條DELETE(刪除)語句。

$this->db->delete('mytable', array('id' => $id));

// 生成:
// DELETE FROM mytable
// WHERE id = $id

第一個參數(shù)是表名,第二個參數(shù)是where子句。你可以不傳遞第二個參數(shù),使用 where() 或者 or_where() 函數(shù)來替代它:

$this->db->where('id', $id);
$this->db->delete('mytable');

// 生成:
// DELETE FROM mytable
// WHERE id = $id

如果你想要從一個以上的表中刪除數(shù)據(jù),你可以將一個包含了多個表名的數(shù)組傳遞給delete()函數(shù)。

$tables = array('table1', 'table2', 'table3');
$this->db->where('id', '5');
$this->db->delete($tables);

如果你想要刪除表中的全部數(shù)據(jù),你可以使用 truncate() 函數(shù),或者 empty_table() 函數(shù)。

$this->db->empty_table();

生成并執(zhí)行一條DELETE(刪除)語句。 $this->db->empty_table('mytable');

// 生成
// DELETE FROM mytable

$this->db->truncate();

生成并執(zhí)行一條TRUNCATE(截斷)語句。

$this->db->from('mytable');
$this->db->truncate();
// 或
$this->db->truncate('mytable');

// 生成:
// TRUNCATE TABLE mytable

說明: 如果 TRUNCATE 命令不可用,truncate() 將會以 "DELETE FROM table" 的方式執(zhí)行。

delete方法貌似現(xiàn)在沒有辦法接收排序參數(shù)

假設(shè)我想刪除早期的用戶登錄日志信息,我的語法可能會是根據(jù)登錄時間正序排列,然后跟上limit已經(jīng)有的總?cè)罩緱l數(shù)減去想保留的條數(shù),delete方法沒有這個參數(shù)用來接收,那么我只能直接使用query傳遞我的sql了

?鏈?zhǔn)椒椒?/h1>

鏈?zhǔn)椒椒ㄔ试S你以連接多個函數(shù)的方式簡化你的語法??紤]一下這個范例:

$this->db->select('title')->from('mytable')->where('id', $id)->limit(10, 20);

$query = $this->db->get();

說明: 鏈?zhǔn)椒椒ㄖ荒茉赑HP 5下面運(yùn)行。

?

?Active Record 緩存

盡管不是 "真正的" 緩存,Active Record 允許你將查詢的某個特定部分保存(或"緩存")起來,以便在你的腳本執(zhí)行之后重用。一般情況下,當(dāng)一次Active Record調(diào)用結(jié)束,所有已存儲的信息都會被重置,以便下一次調(diào)用。如果開啟緩存,你就可以使信息避免被重置,方便你進(jìn)行重用。

緩存調(diào)用是累加的。如果你調(diào)用了兩次有緩存的 select(),然后再調(diào)用兩次沒有緩存的 select(),這會導(dǎo)致 select() 被調(diào)用4次。有三個可用的緩存函數(shù):

$this->db->start_cache()

本函數(shù)必須被用來開啟緩存。所有類型正確的(下面給出了支持的查詢類型) Active Record 查詢都會被存儲起來供以后使用。

$this->db->stop_cache()

本函數(shù)可以被用來停止緩存。

$this->db->flush_cache()

本函數(shù)從Active Record 緩存中刪除全部項目。

這里是一個使用范例:

$this->db->start_cache();
$this->db->select('field1');
$this->db->stop_cache();

$this->db->get('tablename');

//Generates: SELECT `field1` FROM (`tablename`)

$this->db->select('field2');
$this->db->get('tablename');

//Generates: SELECT `field1`, `field2` FROM (`tablename`)

$this->db->flush_cache();

$this->db->select('field2');
$this->db->get('tablename');

//Generates: SELECT `field2` FROM (`tablename`)

說明: 下列語句能夠被緩存: select, from, join, where, like, group_by, having, order_by, set

?

翻譯貢獻(xiàn)者: atans, aykirk, Drice, Fanbin, Hex, houlianshan, huanxiangwu, imjie, k7gxn56, longjianghu, ripen, thankwsx, yinzhili, zhangminghua218
最后修改: 2012-04-17 16:39:22
Artikel sebelumnya: Artikel seterusnya: