?
This document uses PHP Chinese website manual Release
CodeIgniter允許您訪問查詢生成器類。此模式允許以最少的腳本在數(shù)據(jù)庫中檢索、插入和更新信息。在某些情況下,執(zhí)行數(shù)據(jù)庫操作只需要一兩行代碼。CodeIgniter不要求每個數(shù)據(jù)庫表都是自己的類文件。相反,它提供了一個更簡化的接口。
除了簡單性之外,使用QueryBuilder特性的一個主要好處是它允許您創(chuàng)建獨(dú)立于數(shù)據(jù)庫的應(yīng)用程序,因?yàn)椴樵冋Z法是由每個數(shù)據(jù)庫適配器生成的。它還允許更安全的查詢,因?yàn)檫@些值是由系統(tǒng)自動轉(zhuǎn)義的。
注
如果打算編寫自己的查詢,可以在數(shù)據(jù)庫配置文件中禁用該類,從而允許核心數(shù)據(jù)庫庫和適配器利用較少的資源。
選擇數(shù)據(jù)
尋找特定數(shù)據(jù)
尋找相似的數(shù)據(jù)
排序結(jié)果
限制或計(jì)數(shù)結(jié)果
查詢分組
插入數(shù)據(jù)
更新數(shù)據(jù)
刪除數(shù)據(jù)
方法鏈
查詢生成器緩存
重置查詢生成器
類引用
以下函數(shù)允許您構(gòu)建SQL選擇陳述。
$this->db->獲取%28%29
運(yùn)行選擇查詢并返回結(jié)果。它可以用于檢索表中的所有記錄:
$query = $this->db->get('mytable'); // Produces: SELECT * FROM mytable
五參數(shù)使您能夠設(shè)置限制和偏移子句:
$query = $this->db->get('mytable', 10, 20);// Executes: SELECT * FROM mytable LIMIT 20, 10// (in MySQL. Other databases have slightly different syntax)
您將注意到,上面的函數(shù)被分配給一個名為$query的變量,該變量可用于顯示結(jié)果:
$query = $this->db->get('mytable');foreach ($query->result() as $row){ echo $row->title;}
請?jiān)L問結(jié)果函數(shù)頁,以了解有關(guān)結(jié)果生成的完整討論。
$this - > DB-> get_compiled_select()
像$ this-> db-> get()一樣編譯選擇查詢,但不運(yùn)行查詢。該方法只是將SQL查詢作為字符串返回。
例子:
$sql = $this->db->get_compiled_select('mytable');echo $sql;// Prints string: SELECT * FROM mytable
第二個參數(shù)使您可以設(shè)置查詢構(gòu)建器查詢是否將被重置(默認(rèn)情況下,它將被重置,就像使用時一樣$this->db->get()
):
echo $this->db->limit(10,20)->get_compiled_select('mytable', FALSE);// Prints string: SELECT * FROM mytable LIMIT 20, 10// (in MySQL. Other databases have slightly different syntax)echo $this->db->select('title, content, date')->get_compiled_select();// Prints string: SELECT title, content, date FROM mytable LIMIT 20, 10
上面例子中要注意的關(guān)鍵是第二個查詢沒有使用$ this-> db-> from(),并且沒有將表名傳遞給第一個參數(shù)。造成這種結(jié)果的原因是因?yàn)椴樵儾⑽词褂?strong>$ this-> db-> get()來執(zhí)行,這會重置值或直接使用$ this-> db-> reset_query()進(jìn)行重置。
$這個 - > DB-> get_where()
除了可以在第二個參數(shù)中添加“where”子句,而不是使用db-> where()函數(shù)外,其他功能與上述功能相同:
$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);
有關(guān)更多信息,請閱讀下面的WHERE函數(shù)。
注
get_where()以前稱為getwhere(),它已被刪除
$rhis - > DB->select()
允許您編寫查詢的選擇部分:
$this->db->select('title, content, date');$query = $this->db->get('mytable');// Executes: SELECT title, content, date FROM mytable
注
如果您從表中選擇全部(*),則不需要使用此功能。如果省略,CodeIgniter會假設(shè)您希望選擇所有字段并自動添加'SELECT *'。
$this->db->select()
接受可選的第二個參數(shù)。如果將其設(shè)置為false,CodeIgniter將不會試圖保護(hù)您的字段名或表名。如果您需要一個復(fù)合SELECT語句,那么這是非常有用的,其中字段的自動轉(zhuǎn)義可能會破壞它們。
$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'); // Produces: 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'); // Produces: 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'); // Produces: SELECT SUM(age) as age FROM members
$這個 - > DB-from)>(
允許您編寫查詢的FROM部分:
$this->db->select('title, content, date');$this->db->from('mytable');$query = $this->db->get(); // Produces: SELECT title, content, date FROM mytable
注
如前所示,查詢的FROM部分可以在$ this-> db-> get()函數(shù)中指定,因此請使用您喜歡的任何方法。
$this - > DB->join()
允許您編寫查詢的聯(lián)接部分:
$this->db->select('*');$this->db->from('blogs');$this->db->join('comments', 'comments.id = blogs.id');$query = $this->db->get();// Produces:// SELECT * FROM blogs JOIN comments ON comments.id = blogs.id
如果在一個查詢中需要多個聯(lián)接,則可以執(zhí)行多個函數(shù)調(diào)用。
如果需要特定類型的聯(lián)接,可以通過函數(shù)的第三個參數(shù)指定它。選項(xiàng)有:左、右、外、內(nèi)、左、外、右。
$this->db->join('comments', 'comments.id = blogs.id', 'left');// Produces: LEFT JOIN comments ON comments.id = blogs.id
$this- > DB->,where()
此函數(shù)使您能夠設(shè)置何地使用四種方法之一的子句:
注
傳遞給該函數(shù)的所有值都會自動轉(zhuǎn)義,從而產(chǎn)生更安全的查詢。
簡單的鍵/值方法: $ this-> db-> where('name',$ name); //產(chǎn)生:WHERE name ='Joe'注意等號是為你添加的。如果您使用多個函數(shù)調(diào)用,它們將與它們之間的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'
自定義密鑰/值方法:
您可以在第一個參數(shù)中包含一個運(yùn)算符,以便控制比較:
$ this-> db-> where('name!=',$ name); $ this-> db-> where('id <',$ id); //產(chǎn)生:WHERE name!='Joe'AND id <45
關(guān)聯(lián)數(shù)組方法: $ array = array('name'=> $ name,'title'=> $ title,'status'=> $ status); $這- > DB->其中($陣列); //產(chǎn)生:WHERE name ='Joe'AND title ='boss'AND status ='active'你也可以使用這個方法包含你自己的操作符:$ array = array('name!='=> $ name,' id''=> $ id,'date>'=> $ date); $這- > DB->其中($陣列);
自定義字符串: 您可以手動編寫自己的子句:
$ where =“name ='Joe'AND status ='boss'OR status ='active'”; $這 - > DB->其中($其中);
$this->db->where()
接受可選的第三個參數(shù)。如果將其設(shè)置為FALSE,CodeIgniter將不會嘗試保護(hù)您的字段或表名。
$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);
$this - > DB-> or_where()
該函數(shù)與上面的函數(shù)相同,只是多個實(shí)例由OR連接:
$this->db->where('name !=', $name);$this->db->or_where('id >', $id); // Produces: WHERE name != 'Joe' OR id > 50
注
or_where()以前稱為orwhere(),它已被刪除。
$this - > DB-> where_in()
如果合適,生成一個WHERE字段IN('item','item')SQL查詢連接
$names = array('Frank', 'Todd', 'James');$this->db->where_in('username', $names);// Produces: WHERE username IN ('Frank', 'Todd', 'James')
$this- > DB-> or_where_in()
生成一個WHERE字段IN('item','item')SQL查詢,如果合適的話加入OR
$names = array('Frank', 'Todd', 'James');$this->db->or_where_in('username', $names);// Produces: OR username IN ('Frank', 'Todd', 'James')
$this - > DB-> where_not_in()
生成一個WHERE字段NOT IN('item','item')SQL查詢連接AND(如果適用)
$names = array('Frank', 'Todd', 'James');$this->db->where_not_in('username', $names);// Produces: WHERE username NOT IN ('Frank', 'Todd', 'James')
$this - > DB-> or_where_not_in()
生成一個WHERE字段NOT IN('item','item')SQL查詢連接OR,如果適用的話
$names = array('Frank', 'Todd', 'James');$this->db->or_where_not_in('username', $names);// Produces: OR username NOT IN ('Frank', 'Todd', 'James')
$this - > DB-)>like(
此方法使您能夠生成就像子句,用于搜索。
注
傳遞給此方法的所有值都會自動轉(zhuǎn)義。
簡單的鍵/值方法: $ this-> db-> like('title','match'); //產(chǎn)生:WHERE title
LIKE'%match%'ESCAPE'!' 如果您使用多個方法調(diào)用,它們將與它們之間的AND鏈接在一起:$ this-> db-> like('title','match'); $ this-> db-> like('body','match'); // WHERE title
LIKE'%match%'ESCAPE'!' AND body
LIKE'%match%ESCAPE'!' 如果要控制放置通配符(%)的位置,可以使用可選的第三個參數(shù)。您的選項(xiàng)是“之前”,“之后”和“兩者”(這是默認(rèn)設(shè)置)。$ this-> db-> like('title','match','before'); //產(chǎn)生:WHERE title
LIKE'%match'ESCAPE'!' $ this-> db-> like('title',' 匹配','之后'); title
//產(chǎn)生:WHERE LIKE'match%'ESCAPE'!' $ this-> db-> like('title','match','both'); //產(chǎn)生:WHEREtitle
LIKE'%match%'ESCAPE'!'
關(guān)聯(lián)數(shù)組法:
$ array = array('title'=> $ match,'page1'=> $ match,'page2'=> $ match); $這- > DB->像($陣列); // WHERE title
LIKE'%match%'ESCAPE'!' AND page1
LIKE'%match%'ESCAPE'!' AND page2
LIKE'%match%'ESCAPE'!'
$this - > DB-> or_like()
此方法與上面的方法相同,只是多個實(shí)例由OR連接:
$this->db->like('title', 'match'); $this->db->or_like('body', $match);// WHERE `title` LIKE '%match%' ESCAPE '!' OR `body` LIKE '%match%' ESCAPE '!'
注
or_like()
以前被稱為orlike()
,它已經(jīng)被移除了。
$this - > DB-)> not_like(
這種方法與like()
,但它生成的語句不像:
$this->db->not_like('title', 'match'); // WHERE `title` NOT LIKE '%match% ESCAPE '!'
$this - > DB-> or_not_like()
這種方法與not_like()
,除非多個實(shí)例由OR連接:
$this->db->like('title', 'match');$this->db->or_not_like('body', 'match');// WHERE `title` LIKE '%match% OR `body` NOT LIKE '%match%' ESCAPE '!'
$this - > DB-> GROUP_BY()
允許您按查詢的部分寫入組:
$this->db->group_by("title"); // Produces: GROUP BY title
還可以傳遞多個值的數(shù)組:
$this->db->group_by(array("title", "date")); // Produces: GROUP BY title, date
注
group_by()以前稱為groupby(),它已被刪除。
$this- > DB->distinct()
將“DISTISTY”關(guān)鍵字添加到查詢中。
$this->db->distinct();$this->db->get('table'); // Produces: SELECT DISTINCT * FROM table
$this - > DB->having()
允許您編寫查詢的有部分。有兩個可能的語法,一個論點(diǎn)或兩個:
$this->db->having('user_id = 45'); // Produces: HAVING user_id = 45$this->db->having('user_id', 45); // Produces: HAVING user_id = 45
還可以傳遞多個值的數(shù)組:
$this->db->having(array('title =' => 'My Title', 'id <' => $id));// Produces: HAVING title = 'My Title', id < 45
如果使用CodeIgniter轉(zhuǎn)義查詢的數(shù)據(jù)庫,則可以通過傳遞可選的第三個參數(shù)并將其設(shè)置為false來防止轉(zhuǎn)義內(nèi)容。
$this->db->having('user_id', 45); // Produces: HAVING `user_id` = 45 in some databases such as MySQL$this->db->having('user_id', 45, FALSE); // Produces: HAVING user_id = 45
$this - > DB-> or_having()
與having()相同,僅用“OR”分隔多個子句。
$this - > DB-> ORDER_BY()
允許您設(shè)置ORDERBY子句。
第一個參數(shù)包含要按其排序的列的名稱。
第二個參數(shù)可讓您設(shè)置結(jié)果的方向。選項(xiàng)是ASC,DESC和RANDOM。
$this->db->order_by('title', 'DESC');// Produces: ORDER BY `title` DESC
您還可以在第一個參數(shù)中傳遞您自己的字符串:
$this->db->order_by('title DESC, name ASC');// Produces: ORDER BY `title` DESC, `name` ASC
或者,如果需要多個字段,則可以進(jìn)行多個函數(shù)調(diào)用。
$this->db->order_by('title', 'DESC');$this->db->order_by('name', 'ASC');// Produces: ORDER BY `title` DESC, `name` ASC
如果您選擇隨機(jī)選項(xiàng),則第一個參數(shù)將被忽略,除非指定一個數(shù)值種子值。
$this->db->order_by('title', 'RANDOM');// Produces: ORDER BY RAND()$this->db->order_by(42, 'RANDOM');// Produces: ORDER BY RAND(42)
注
order_by()以前稱為orderby(),它已被刪除。
注
Oracle目前不支持隨機(jī)排序,而是默認(rèn)為ASC。
$this->db->limit()
允許您限制希望由查詢返回的行數(shù):
$this->db->limit(10); // Produces: LIMIT 10
第二個參數(shù)允許您設(shè)置結(jié)果偏移量。
$this->db->limit(10, 20); // Produces: LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
$this->db->count_all_results()
允許您確定特定活動記錄查詢中的行數(shù)。查詢將接受查詢生成器限制項(xiàng),如where()
,,,or_where()
,,,like()
,,,or_like()
,例如:
echo $this->db->count_all_results('my_table'); // Produces an integer, like 25$this->db->like('title', 'match');$this->db->from('my_table');echo $this->db->count_all_results(); // Produces an integer, like 17
但是,此方法還重置您可能傳遞給的任何字段值。select()
如果你需要保存它們,你可以通過FALSE
作為第二個參數(shù):
echo $this->db->count_all_results('my_table', FALSE);
$this->db->count_all()
允許您確定特定表中的行數(shù)。在第一個參數(shù)中提交表名。例子:
echo $this->db->count_all('my_table'); // Produces an integer, like 25
查詢分組允許您通過將WHERE子句括在括號中來創(chuàng)建WHERE子句組。這將允許您創(chuàng)建具有復(fù)雜WHERE子句的查詢。支持嵌套組。例子:
$this->db->select('*')->from('my_table') ->group_start() ->where('a', 'a') ->or_group_start() ->where('b', 'b') ->where('c', 'c') ->group_end() ->group_end() ->where('d', 'd')->get();// Generates:// SELECT * FROM (`my_table`) WHERE ( `a` = 'a' OR ( `b` = 'b' AND `c` = 'c' ) ) AND `d` = 'd'
注
組需要平衡,確保每個group_start()都被一個group_end()匹配。
$this->db->group_start()
通過在查詢的WHERE子句中添加一個括號開始一個新組。
$this->db->or_group_start()
通過在查詢的WHERE子句中添加一個括號,以‘OR’作為前綴,啟動一個新組。
$this->db->not_group_start()
通過在查詢的WHERE子句中添加一個括號,以“NOT”作為前綴,開始一個新的組。
$this->db->or_not_group_start()
通過在查詢的WHERE子句中添加一個括號開始一個新的組,并以‘or no’作為前綴。
$this->db->group_end()
通過向查詢的WHERE子句添加一個結(jié)束括號來結(jié)束當(dāng)前組。
$this->db->insert()
根據(jù)所提供的數(shù)據(jù)生成插入字符串,并運(yùn)行查詢。您可以通過一個列陣或者對象為了這個功能。下面是一個使用數(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ù)是值的關(guān)聯(lián)數(shù)組。
下面是一個使用對象的示例:
/* class Myclass { public $title = 'My Title'; public $content = 'My Content'; public $date = 'My Date'; } */$object = new Myclass;$this->db->insert('mytable', $object);// Produces: INSERT INTO mytable (title, content, date) VALUES ('My Title', 'My Content', 'My Date')
第一個參數(shù)將包含表名,第二個參數(shù)將包含一個對象。
注
所有值都會自動轉(zhuǎn)義,從而產(chǎn)生更安全的查詢。
$this->db->get_compiled_insert()
像$ this-> db-> insert()一樣編譯插入查詢,但不運(yùn)行查詢。該方法只是將SQL查詢作為字符串返回。
例子:
$data = array( 'title' => 'My title', 'name' => 'My Name', 'date' => 'My date');$sql = $this->db->set($data)->get_compiled_insert('mytable');echo $sql;// Produces string: INSERT INTO mytable (`title`, `name`, `date`) VALUES ('My title', 'My name', 'My date')
第二個參數(shù)使您可以設(shè)置查詢構(gòu)建器查詢是否將被重置(默認(rèn)情況下它將與$ this-> db-> insert()一樣):
echo $this->db->set('title', 'My Title')->get_compiled_insert('mytable', FALSE);// Produces string: INSERT INTO mytable (`title`) VALUES ('My Title')echo $this->db->set('content', 'My Content')->get_compiled_insert();// Produces string: INSERT INTO mytable (`title`, `content`) VALUES ('My Title', 'My Content')
在上面的例子中需要注意的關(guān)鍵是第二個查詢沒有使用$this->db->from()
它也沒有將表名傳遞給第一個參數(shù)。此操作之所以有效,是因?yàn)闆]有使用$this->db->insert()
使用以下方法重置值或直接重置$this->db->reset_query()
...
注
此方法不適用于批處理插入。
$this - > DB-> insert_batch()
根據(jù)所提供的數(shù)據(jù)生成插入字符串,并運(yùn)行查詢。您可以通過一個列陣或者對象為了這個功能。下面是一個使用數(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); // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')
第一個參數(shù)將包含表名,第二個參數(shù)是值的關(guān)聯(lián)數(shù)組。
注
所有值都會自動轉(zhuǎn)義,從而產(chǎn)生更安全的查詢。
$this->db->replace()
此方法執(zhí)行REPLACE語句,該語句基本上是(可選的)DELETE + INSERT的SQL標(biāo)準(zhǔn),使用PRIMARY和UNIQUE鍵作為決定性因素。在我們的例子中,它可以使你免于需要實(shí)現(xiàn)與不同的組合復(fù)雜的邏輯select()
,update()
,delete()
和insert()
電話。
例子:
$data = array( 'title' => 'My title', 'name' => 'My Name', 'date' => 'My date'); $this->db->replace('table', $data);// Executes: REPLACE INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
在上面的示例中,如果我們假設(shè)標(biāo)題字段是我們的主鍵,那么如果一行包含“我的標(biāo)題”作為標(biāo)題值時,將刪除該行,并替換新的行數(shù)據(jù)。
使用set()
方法,并且所有字段都會自動轉(zhuǎn)義,如下所示insert()
...
$this->db->set()
此函數(shù)使您能夠?yàn)椴迦牖蚋略O(shè)置值。
可以使用它而不是直接將數(shù)據(jù)數(shù)組傳遞給INSERT或UPDATE函數(shù):
$this->db->set('name', $name);$this->db->insert('mytable'); // Produces: INSERT INTO mytable (`name`) VALUES ('{$name}')
如果您使用多個調(diào)用的函數(shù),則將根據(jù)您正在執(zhí)行的插入或更新正確地組裝它們:
$this->db->set('name', $name);$this->db->set('title', $title);$this->db->set('status', $status);$this->db->insert('mytable');
set()也會接受一個可選的第三個參數(shù)($escape
),如果設(shè)置為FALSE,這將防止數(shù)據(jù)被轉(zhuǎn)義。為了說明不同之處,這里set()
使用和不使用escape參數(shù)。
$this->db->set('field', 'field+1', FALSE);$this->db->where('id', 2);$this->db->update('mytable'); // gives UPDATE mytable SET field = field+1 WHERE id = 2$this->db->set('field', 'field+1');$this->db->where('id', 2);$this->db->update('mytable'); // gives UPDATE `mytable` SET `field` = 'field+1' WHERE `id` = 2
還可以將關(guān)聯(lián)數(shù)組傳遞給此函數(shù):
$array = array( 'name' => $name, 'title' => $title, 'status' => $status); $this->db->set($array); $this->db->insert('mytable');
或物體:
/* class Myclass { public $title = 'My Title'; public $content = 'My Content'; public $date = 'My Date'; } */$object = new Myclass;$this->db->set($object);$this->db->insert('mytable');
$this->db->update()
生成更新字符串,并根據(jù)所提供的數(shù)據(jù)運(yùn)行查詢。你可以通過一個列陣或者對象為了這個功能。下面是一個使用數(shù)組的示例:
$data = array( 'title' => $title, 'name' => $name, 'date' => $date); $this->db->where('id', $id); $this->db->update('mytable', $data);// Produces://// UPDATE mytable// SET title = '{$title}', name = '{$name}', date = '{$date}'// WHERE id = $id
或者您可以提供一個對象:
/* class Myclass { public $title = 'My Title'; public $content = 'My Content'; public $date = 'My Date'; } */ $object = new Myclass; $this->db->where('id', $id); $this->db->update('mytable', $object);// Produces://// UPDATE `mytable`// SET `title` = '{$title}', `name` = '{$name}', `date` = '{$date}'// WHERE id = `$id`
注
所有值都會自動轉(zhuǎn)義,從而產(chǎn)生更安全的查詢。
您會注意到使用$ this-> db-> where()函數(shù),使您可以設(shè)置WHERE子句。您可以選擇將此信息直接作為字符串傳遞給更新函數(shù):
$this->db->update('mytable', $data, "id = 4");
或者作為一個數(shù)組:
$this->db->update('mytable', $data, array('id' => $id));
執(zhí)行更新時,您也可以使用上述的$ this-> db-> set()函數(shù)。
$this - > DB-> update_batch()
根據(jù)所提供的數(shù)據(jù)生成更新字符串,并運(yùn)行查詢。您可以通過一個列陣或者對象為了這個功能。下面是一個使用數(shù)組的示例:
$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ù)將包含表名,第二個參數(shù)是值的關(guān)聯(lián)數(shù)組,第三個參數(shù)是WHERE鍵。
注
所有值都會自動轉(zhuǎn)義,從而產(chǎn)生更安全的查詢。
注
affected_rows()
由于它的工作原理,它不會給出正確的結(jié)果。相反,update_batch()
返回受影響的行數(shù)。
$this->db->get_compiled_update()
它的工作方式與$this->db->get_compiled_insert()
但是,它生成一個更新SQL字符串,而不是插入SQL字符串。
注
此方法不適用于批處理更新。
$this->db->delete()
生成一個DELETE SQL字符串并運(yùn)行查詢。
$this->db->delete('mytable', array('id' => $id)); // Produces: // DELETE FROM mytable // WHERE id = $id
第一個參數(shù)是表名,第二個參數(shù)是where子句。您也可以使用where()或or_where()函數(shù),而不是將數(shù)據(jù)傳遞給函數(shù)的第二個參數(shù):
$this->db->where('id', $id);$this->db->delete('mytable');// Produces:// DELETE FROM mytable// WHERE id = $id
如果要從多個表中刪除數(shù)據(jù),可以將一個表名數(shù)組傳遞給delete()。
$tables = array('table1', 'table2', 'table3');$this->db->where('id', '5');$this->db->delete($tables);
如果要刪除表中的所有數(shù)據(jù),可以使用truncate()函數(shù)或empty_table()。
$this->db->empty_table()
生成刪除SQL字符串并運(yùn)行查詢。
$this->db->empty_table('mytable'); // Produces: DELETE FROM mytable
$this->db->truncate()
生成截?cái)郤QL字符串并運(yùn)行查詢。
$this->db->from('mytable');$this->db->truncate();// or$this->db->truncate('mytable');// Produce:// TRUNCATE mytable
注
如果TRUNCATE命令不可用,truncate()將作為“DELETE FROM table”執(zhí)行。
$this->db->get_compiled_delete()
它的工作方式與$this->db->get_compiled_insert()
但是,它生成一個DELETE SQL字符串,而不是INSERT SQL字符串。
有關(guān)更多信息,請查看$ this-> db-> get_compiled_insert()的文檔。
方法鏈接允許您通過連接多個函數(shù)來簡化語法。考慮這個例子:
$query = $this->db->select('title') ->where('id', $id) ->limit(10, 20) ->get('mytable');
雖然不是“真正的”緩存,但查詢生成器使您可以保存(或“緩存”)查詢的某些部分,以便稍后在腳本執(zhí)行過程中重新使用。通常情況下,當(dāng)查詢構(gòu)建器調(diào)用完成時,所有存儲的信息都會重置以用于下一次調(diào)用。通過緩存,您可以防止此重置,并輕松地重用信息。
緩存的呼叫是累積的。如果進(jìn)行2次緩存的select()調(diào)用,然后進(jìn)行2次未緩存的select()調(diào)用,則會導(dǎo)致4次select()調(diào)用。有三種緩存功能可用:
$this->db->start_cache()
必須調(diào)用此函數(shù)才能開始緩存。所有正確類型的查詢生成器查詢(請參閱下面的支持的查詢)都存儲起來供以后使用。
$this->db->stop_cache()
可以調(diào)用此函數(shù)來停止緩存。
$this->db->flush_cache()
此函數(shù)從查詢生成器緩存中刪除所有項(xiàng)。
下面是一個用法示例:
$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,group_by,having,order_by
$this - > DB-> reset_query()
重置查詢生成器允許你重新啟動你的查詢,而不用先執(zhí)行它,像$ this-> db-> get()或$ this-> db-> insert()。就像執(zhí)行查詢的方法一樣,這不會重置使用查詢生成器緩存進(jìn)行緩存的項(xiàng)目。
這在您使用查詢生成器生成SQL(例如$this->db->get_compiled_select()
)但是然后選擇運(yùn)行查詢的情況下非常有用:
// Note that the second parameter of the get_compiled_select method is FALSE$sql = $this->db->select(array('field1','field2')) ->where('field3',5) ->get_compiled_select('mytable', FALSE);// ...// Do something crazy with the SQL code... like add it to a cron script for// later execution or something...// ...$data = $this->db->get()->result_array();// Would execute and return an array of results of the following query:// SELECT field1, field1 from mytable where field3 = 5;
注
雙呼get_compiled_select()
當(dāng)您使用QueryBuilder緩存功能而不重置查詢時,將導(dǎo)致緩存合并兩次。反過來,如果你在緩存一個select()
-兩次選擇同一個字段。
class CI_DB_query_builderreset_query()
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
---|---|
返回類型: | CI_DB_query_builder |
start_cache()
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
---|---|
返回類型: | CI_DB_query_builder |
stop_cache()
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
---|---|
返回類型: | CI_DB_query_builder |
flush_cache()
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
---|---|
返回類型: | CI_DB_query_builder |
set_dbprefix([$prefix = ''])
參數(shù): | $ prefix(string) - 要使用的新前綴 |
---|---|
返回: | 數(shù)據(jù)庫前綴正在使用中 |
返回類型: | 串 |
$ prefix(string) - 要使用的新前綴返回:正在使用的數(shù)據(jù)庫前綴返回類型:字符串設(shè)置數(shù)據(jù)庫前綴,而不必重新連接。dbprefix([$table = ''])
參數(shù):$ table(string) - 表名稱prefixReturns:前綴表名返回類型:字符串
$ table(string) - 表格名稱作為前綴
Returns: The prefixed table name
Return type: string
Prepends a database prefix, if one exists in configuration.
count_all_results([$table = ''[, $reset = TRUE]])
參數(shù): | $ table(string) - 表名$ reset(bool) - 是否重置SELECTs的值 |
---|---|
返回: | 查詢結(jié)果中的行數(shù) |
返回類型: | INT |
$ table(string) - 表名
$ reset(bool) - 是否重置SELECT的值
Returns: Number of rows in the query result
Return type: int
Generates a platform-specific query string that counts all records returned by an Query Builder query.
get([$table = ''[, $limit = NULL[, $offset = NULL]]])
參數(shù): | $ table(string) - 要查詢的表$ limit(int) - LIMIT子句$ offset(int) - OFFSET子句 |
---|---|
返回: | CI_DB_result實(shí)例(方法鏈接) |
返回類型: | CI_DB_result |
$ table(string) - 要查詢的表
$ limit(int) - LIMIT子句
$ offset(int) - OFFSET子句
Returns: CI\_DB\_result instance (method chaining)
Return type: CI\_DB\_result
Compiles and runs SELECT statement based on the already called Query Builder methods.
get_where([$table = ''[, $where = NULL[, $limit = NULL[, $offset = NULL]]]])
參數(shù): | $ table(mixed) - 從中獲取數(shù)據(jù)的表格; 字符串或數(shù)組$ where(string) - WHERE子句$ limit(int) - LIMIT子句$ offset(int) - OFFSET子句 |
---|---|
返回: | CI_DB_result實(shí)例(方法鏈接) |
返回類型: | CI_DB_result |
$ table(mixed) - 從中獲取數(shù)據(jù)的表格; 字符串或數(shù)組
$ where(string) - WHERE子句
$ limit(int) - LIMIT子句
$ offset(int) - OFFSET子句
Returns: CI\_DB\_result instance (method chaining)
Return type: CI\_DB\_result
Same as `get()`, but also allows the WHERE to be added directly.
select([$select = '*'[, $escape = NULL]])
參數(shù): | $ select(string) - 查詢的SELECT部分$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ select(string) - 查詢的SELECT部分
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds a SELECT clause to a query.
select_avg([$select = ''[, $alias = '']])
參數(shù): | $ select(string) - 用于計(jì)算$ alias(字符串)的平均值的字段 - 結(jié)果值名稱的別名 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ select(string) - Field來計(jì)算平均值
$ alias(string) - 結(jié)果值名稱的別名
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds a SELECT AVG(field) clause to a query.
select_max([$select = ''[, $alias = '']])
參數(shù): | $ select(string) - 用于計(jì)算$ alias(字符串)的最大值的字段 - 結(jié)果值名稱的別名 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ select(string) - Field來計(jì)算最大值
$ alias(string) - 結(jié)果值名稱的別名
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds a SELECT MAX(field) clause to a query.
select_min([$select = ''[, $alias = '']])
參數(shù): | $ select(string) - 用于計(jì)算$ alias(字符串)的最小值的字段 - 結(jié)果值名稱的別名 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ select(string) - Field來計(jì)算最小值
$ alias(string) - 結(jié)果值名稱的別名
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds a SELECT MIN(field) clause to a query.
select_sum([$select = ''[, $alias = '']])
參數(shù): | $ select(string) - 用于計(jì)算$ alias(字符串)的總和的字段 - 結(jié)果值名稱的別名 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ select(string) - Field來計(jì)算總和
$ alias(string) - 結(jié)果值名稱的別名
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds a SELECT SUM(field) clause to a query.
distinct([$val = TRUE])
參數(shù): | $ val(bool) - “distinct”標(biāo)志的期望值 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ val(bool) - “distinct”標(biāo)志的期望值返回:CI_DB_query_builder實(shí)例(方法鏈)返回類型:CI_DB_query_builder設(shè)置一個標(biāo)志,通知查詢構(gòu)建器將DISTINCT子句添加到查詢的SELECT部分。from($from)
參數(shù):$ from(mixed) - 表名(s); 字符串或數(shù)組返回:CI_DB_query_builder實(shí)例(方法鏈)返回類型:CI_DB_query_builder
$ from(mixed) - 表名(s); 字符串或數(shù)組
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Specifies the FROM clause of a query.
join($table, $cond[, $type = ''[, $escape = NULL]])
參數(shù): | $ table(string) - 表名加入$ cond(string) - JOIN ON條件$ type(string) - JOIN類型$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ table(string) - 表名加入
$ cond(string) - JOIN ON條件
$ type(string) - JOIN類型
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds a JOIN clause to a query.
where($key[, $value = NULL[, $escape = NULL]])
參數(shù): | $ key(mixed) - 要比較的字段的名稱或關(guān)聯(lián)數(shù)組$ value(mixed) - 如果與此值進(jìn)行比較的單個鍵$ escape(bool) - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | DB_query_builder實(shí)例 |
返回類型: | 目的 |
$ key(mixed) - 要比較的字段的名稱或關(guān)聯(lián)數(shù)組
$ value(混合) - 如果單個鍵與此值相比較
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: DB\_query\_builder instance
Return type: object
Generates the WHERE portion of the query. Separates multiple calls with ‘AND’.
or_where($key[, $value = NULL[, $escape = NULL]])
參數(shù): | $ key(mixed) - 要比較的字段的名稱或關(guān)聯(lián)數(shù)組$ value(mixed) - 如果與此值進(jìn)行比較的單個鍵$ escape(bool) - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | DB_query_builder實(shí)例 |
返回類型: | 目的 |
$ key(mixed) - 要比較的字段的名稱或關(guān)聯(lián)數(shù)組
$ value(混合) - 如果單個鍵與此值相比較
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: DB\_query\_builder instance
Return type: object
Generates the WHERE portion of the query. Separates multiple calls with ‘OR’.
or_where_in([$key = NULL[, $values = NULL[, $escape = NULL]]])
參數(shù): | $ key(string) - 要搜索的字段$ values(array) - 在$ escape(bool)上搜索的值 - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | DB_query_builder實(shí)例 |
返回類型: | object |
$ key(string) - 要搜索的字段
$ values(array) - 搜索的值
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: DB\_query\_builder instance
Return type: object
Generates a WHERE field IN(‘item’, ‘item’) SQL query, joined with ‘OR’ if appropriate.
or_where_not_in([$key = NULL[, $values = NULL[, $escape = NULL]]])
參數(shù): | $ key(string) - 要搜索的字段$ values(array) - 在$ escape(bool)上搜索的值 - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | DB_query_builder實(shí)例 |
返回類型: | 目的 |
$ key(string) - 要搜索的字段
$ values(array) - 搜索的值
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: DB\_query\_builder instance
Return type: object
Generates a WHERE field NOT IN(‘item’, ‘item’) SQL query, joined with ‘OR’ if appropriate.
where_in([$key = NULL[, $values = NULL[, $escape = NULL]]])
參數(shù): | $ key(string) - 要檢查的字段名稱values(array) - 目標(biāo)值數(shù)組$ escape(bool) - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | DB_query_builder實(shí)例 |
返回類型: | 目的 |
$ key(string) - 要檢查的字段的名稱
$ values(array) - 目標(biāo)值數(shù)組
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: DB\_query\_builder instance
Return type: object
Generates a WHERE field IN(‘item’, ‘item’) SQL query, joined with ‘AND’ if appropriate.
where_not_in([$key = NULL[, $values = NULL[, $escape = NULL]]])
參數(shù): | $ key(string) - 要檢查的字段名稱values(array) - 目標(biāo)值數(shù)組$ escape(bool) - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | DB_query_builder實(shí)例 |
返回類型: | 目的 |
$ key(string) - 要檢查的字段的名稱
$ values(array) - 目標(biāo)值數(shù)組
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: DB\_query\_builder instance
Return type: object
Generates a WHERE field NOT IN(‘item’, ‘item’) SQL query, joined with ‘AND’ if appropriate.
group_start()
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
---|---|
返回類型: | CI_DB_query_builder |
or_group_start()
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
---|---|
返回類型: | CI_DB_query_builder |
not_group_start()
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
---|---|
返回類型: | CI_DB_query_builder |
or_not_group_start()
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
---|---|
返回類型: | CI_DB_query_builder |
group_end()
返回: | DB_query_builder實(shí)例 |
---|---|
返回類型: | 目的 |
like($field[, $match = ''[, $side = 'both'[, $escape = NULL]]])
參數(shù): | $ field(string) - 字段名稱$ match(字符串) - 文本部分匹配$ side(字符串) - 表達(dá)式的哪一邊將'%'通配符放在$ escape(bool)上 - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ field(string) - 字段名稱
$ match(string) - 要匹配的文本部分
$ side(string) - 表達(dá)式的哪一邊放置'%'通配符
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds a LIKE clause to a query, separating multiple calls with AND.
or_like($field[, $match = ''[, $side = 'both'[, $escape = NULL]]])
參數(shù): | $ field(string) - 字段名稱$ match(字符串) - 文本部分匹配$ side(字符串) - 表達(dá)式的哪一邊將'%'通配符放在$ escape(bool)上 - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ field(string) - 字段名稱
$ match(string) - 要匹配的文本部分
$ side(string) - 表達(dá)式的哪一邊放置'%'通配符
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds a LIKE clause to a query, separating multiple class with OR.
not_like($field[, $match = ''[, $side = 'both'[, $escape = NULL]]])
參數(shù): | $ field(string) - 字段名稱$ match(字符串) - 文本部分匹配$ side(字符串) - 表達(dá)式的哪一邊將'%'通配符放在$ escape(bool)上 - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ field(string) - 字段名稱
$ match(string) - 要匹配的文本部分
$ side(string) - 表達(dá)式的哪一邊放置'%'通配符
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds a NOT LIKE clause to a query, separating multiple calls with AND.
or_not_like($field[, $match = ''[, $side = 'both'[, $escape = NULL]]])
參數(shù): | $ field(string) - 字段名稱$ match(字符串) - 文本部分匹配$ side(字符串) - 表達(dá)式的哪一邊將'%'通配符放在$ escape(bool)上 - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ field(string) - 字段名稱
$ match(string) - 要匹配的文本部分
$ side(string) - 表達(dá)式的哪一邊放置'%'通配符
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds a NOT LIKE clause to a query, separating multiple calls with OR.
having($key[, $value = NULL[, $escape = NULL]])
參數(shù): | $ key(字符串) - 如果$ key是標(biāo)識符,則尋找的值$ escape(string) - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ key(混合) - 字段/值對的標(biāo)識符(字符串)或關(guān)聯(lián)數(shù)組
$ value(string) - 如果$ key是標(biāo)識符,則查找值
$ escape(string) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds a HAVING clause to a query, separating multiple calls with AND.
or_having($key[, $value = NULL[, $escape = NULL]])
參數(shù): | $ key(字符串) - 如果$ key是標(biāo)識符,則尋找的值$ escape(string) - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ key(混合) - 字段/值對的標(biāo)識符(字符串)或關(guān)聯(lián)數(shù)組
$ value(string) - 如果$ key是標(biāo)識符,則查找值
$ escape(string) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds a HAVING clause to a query, separating multiple calls with OR.
group_by($by[, $escape = NULL])
參數(shù): | $ by(mixed) - Field to(s)to group by; 字符串或數(shù)組 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ by(mixed) - Field to(s)to group by; 字符串或數(shù)組返回:CI_DB_query_builder實(shí)例(方法鏈)返回類型:CI_DB_query_builder向查詢添加GROUP BY子句。order_by($orderby[, $direction = ''[, $escape = NULL]])
參數(shù):$ orderby(string) - 按照$ direction排序的字段(字符串) - 請求的順序 - ASC,DESC或隨機(jī)$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符返回:CI_DB_query_builder實(shí)例(方法鏈)返回類型:CI_DB_query_builder
$ orderby(string) - 要排序的字段
$方向(字符串) - 請求的順序 - ASC,DESC或隨機(jī)
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds an ORDER BY clause to a query.
limit($value[, $offset = 0])
參數(shù): | $ value(int) - 將結(jié)果限制到$ offset的行數(shù)(int) - 要跳過的行數(shù) |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ value(int) - 將結(jié)果限制到的行數(shù)
$ offset(int) - 要跳過的行數(shù)
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds LIMIT and OFFSET clauses to a query.
offset($offset)
參數(shù): | $ offset(int) - 要跳過的行數(shù) |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ offset(int) - 要跳過的行數(shù)返回:CI_DB_query_builder實(shí)例(方法鏈)返回類型:CI_DB_query_builder向查詢添加OFFSET子句。set($key[, $value = ''[, $escape = NULL]])
參數(shù):$ key(mixed) - 字段名稱或字段/值對的數(shù)組$ value(string) - 字段值,如果$ key是單個字段$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符返回:CI_DB_query_builder實(shí)例(方法鏈)返回類型:CI_DB_query_builder
$ key(mixed) - 字段名稱或字段/值對的數(shù)組
$ value(字符串) - 字段值,如果$ key是單個字段
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds field/value pairs to be passed later to `insert()`, `update()` or `replace()`.
insert([$table = ''[, $set = NULL[, $escape = NULL]]])
參數(shù): | $ table(string) - 表名$ set(array) - 一個字段/值對的關(guān)聯(lián)數(shù)組$ escape(bool) - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ table(string) - 表名
$ set(array) - 一個字段/值對的關(guān)聯(lián)數(shù)組
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: TRUE on success, FALSE on failure
Return type: bool
Compiles and executes an INSERT statement.
insert_batch($table[, $set = NULL[, $escape = NULL[, $batch_size = 100]]])
參數(shù): | $ table(string) - 表名$ set(array) - 要插入的數(shù)據(jù)$ escape(bool) - 是否要轉(zhuǎn)義值和標(biāo)識符$ batch_size(int) - 要一次插入的行數(shù) |
---|---|
返回: | 插入的行數(shù)或失敗時的FALSE |
返回類型: | mixed |
$ table(string) - 表名
$ set(array) - 要插入的數(shù)據(jù)
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
$ batch_size(int) - 一次插入的行數(shù)
Returns: Number of rows inserted or FALSE on failure
Return type: mixed
Compiles and executes batch `INSERT` statements.
注
當(dāng)超過$batch_size
提供多個行INSERT
查詢將被執(zhí)行,每個查詢都試圖插入$batch_size
一排排。
set_insert_batch($key[, $value = ''[, $escape = NULL]])
參數(shù): | $ key(mixed) - 字段名稱或字段/值對數(shù)組$ value(string) - 字段值,如果$ key是單個字段$ escape(bool) - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ key(mixed) - 字段名稱或字段/值對的數(shù)組
$ value(字符串) - 字段值,如果$ key是單個字段
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds field/value pairs to be inserted in a table later via `insert_batch()`.
update([$table = ''[, $set = NULL[, $where = NULL[, $limit = NULL]]]])
參數(shù): | $ table(string) - 表名$ set(array) - 一個字段/值對的關(guān)聯(lián)數(shù)組$ where(string) - WHERE子句$ limit(int) - LIMIT子句 |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ table(string) - 表名
$ set(array) - 一個字段/值對的關(guān)聯(lián)數(shù)組
$ where(string) - WHERE子句
$ limit(int) - LIMIT子句
Returns: TRUE on success, FALSE on failure
Return type: bool
Compiles and executes an UPDATE statement.
update_batch($table[, $set = NULL[, $value = NULL[, $batch_size = 100]]])
參數(shù): | $ table(string) - 表名$ set(array) - 字段名或字段/值對的關(guān)聯(lián)數(shù)組$ value(string) - 字段值,如果$ set是單個字段$ batch_size(int) - 條件以在單個查詢中分組 |
---|---|
返回: | 更新的行數(shù)或失敗時的FALSE |
返回類型: | 雜 |
$ table(string) - 表名
$ set(array) - 字段名稱或字段/值對的關(guān)聯(lián)數(shù)組
$ value(字符串) - 字段值,如果$ set是單個字段
$ batch_size(int) - 要在單個查詢中分組的條件數(shù)
Returns: Number of rows updated or FALSE on failure
Return type: mixed
Compiles and executes batch `UPDATE` statements.
注
當(dāng)超過$batch_size
提供了字段/值對,將執(zhí)行多個查詢,每次處理$batch_size
字段/值對。
set_update_batch($key[, $value = ''[, $escape = NULL]])
參數(shù): | $ key(mixed) - 字段名稱或字段/值對數(shù)組$ value(string) - 字段值,如果$ key是單個字段$ escape(bool) - 是否要轉(zhuǎn)義值和標(biāo)識符 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈) |
返回類型: | CI_DB_query_builder |
$ key(mixed) - 字段名稱或字段/值對的數(shù)組
$ value(字符串) - 字段值,如果$ key是單個字段
$ escape(bool) - 是否轉(zhuǎn)義值和標(biāo)識符
Returns: CI\_DB\_query\_builder instance (method chaining)
Return type: CI\_DB\_query\_builder
Adds field/value pairs to be updated in a table later via `update_batch()`.
replace([$table = ''[, $set = NULL]])
參數(shù): | $ table(string) - 表名$ set(array) - 一個字段/值對的關(guān)聯(lián)數(shù)組 |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ table(string) - 表名
$ set(array) - 一個字段/值對的關(guān)聯(lián)數(shù)組
Returns: TRUE on success, FALSE on failure
Return type: bool
Compiles and executes a REPLACE statement.
delete([$table = ''[, $where = ''[, $limit = NULL[, $reset_data = TRUE]]]])
參數(shù): | $ table(mixed) - 從中刪除的表格; 字符串或數(shù)組$ where(string) - WHERE子句$ limit(int) - LIMIT子句$ reset_data(bool) - TRUE重置查詢“寫入”子句 |
---|---|
返回: | CI_DB_query_builder實(shí)例(方法鏈)或失敗時為FALSE |
返回類型: | 雜 |
$ table(mixed) - 從中刪除的表格; 字符串或數(shù)組
$ where(string) - WHERE子句
$ limit(int) - LIMIT子句
$ reset_data(bool) - TRUE重置查詢“寫入”子句
Returns: CI\_DB\_query\_builder instance (method chaining) or FALSE on failure
Return type: mixed
Compiles and executes a DELETE query.
truncate([$table = ''])
參數(shù): | $ table(string) - 表名 |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ table(string) - 表名返回:成功時為TRUE,失敗時為FALSE返回類型:BOOL在表上執(zhí)行TRUNCATE語句。注意如果正在使用的數(shù)據(jù)庫平臺不支持TRUNCATE,則將使用DELETE語句。empty_table([$table = ''])
參數(shù):$ table(string) - 表名返回:成功時為TRUE,失敗時返回FALSE返回類型:bool
$ table(string) - 表名
Returns: TRUE on success, FALSE on failure
Return type: bool
Deletes all records from a table via a DELETE statement.
get_compiled_select([$table = ''[, $reset = TRUE]])
參數(shù): | $ table(string) - 表名$ reset(bool) - 是否重置當(dāng)前QB值 |
---|---|
返回: | 編譯后的SQL語句為一個字符串 |
返回類型: | 串 |
$ table(string) - 表名
$ reset(bool) - 是否重置當(dāng)前的QB值
Returns: The compiled SQL statement as a string
Return type: string
Compiles a SELECT statement and returns it as a string.
get_compiled_insert([$table = ''[, $reset = TRUE]])
參數(shù): | $ table(string) - 表名$ reset(bool) - 是否重置當(dāng)前QB值 |
---|---|
返回: | 編譯后的SQL語句為一個字符串 |
返回類型: | 串 |
$ table(string) - 表名
$ reset(bool) - 是否重置當(dāng)前的QB值
Returns: The compiled SQL statement as a string
Return type: string
Compiles an INSERT statement and returns it as a string.
get_compiled_update([$table = ''[, $reset = TRUE]])
參數(shù): | $ table(string) - 表名$ reset(bool) - 是否重置當(dāng)前QB值 |
---|---|
返回: | 編譯后的SQL語句為一個字符串 |
返回類型: | 串 |
$ table(string) - 表名
$ reset(bool) - 是否重置當(dāng)前的QB值
Returns: The compiled SQL statement as a string
Return type: string
Compiles an UPDATE statement and returns it as a string.
get_compiled_delete([$table = ''[, $reset = TRUE]])
參數(shù): | $ table(string) - 表名$ reset(bool) - 是否重置當(dāng)前QB值 |
---|---|
返回: | 編譯后的SQL語句為一個字符串 |
返回類型: | 串 |
$ table(string) - 表名
$ reset(bool) - 是否重置當(dāng)前的QB值
返回:編譯后的SQL語句作為字符串
Return type: string
編譯DELETE語句并將其作為字符串返回。