非常教程

Codeigniter 3参考手册

数据库 | Database

查询构建类 | Query Builder Class

CodeIgniter允许您访问查询生成器类。此模式允许以最少的脚本在数据库中检索、插入和更新信息。在某些情况下,执行数据库操作只需要一两行代码。CodeIgniter不要求每个数据库表都是自己的类文件。相反,它提供了一个更简化的接口。

除了简单性之外,使用QueryBuilder特性的一个主要好处是它允许您创建独立于数据库的应用程序,因为查询语法是由每个数据库适配器生成的。它还允许更安全的查询,因为这些值是由系统自动转义的。

如果打算编写自己的查询,可以在数据库配置文件中禁用该类,从而允许核心数据库库和适配器利用较少的资源。

  • 选择数据
  • 寻找特定数据
  • 寻找相似的数据
  • 排序结果
  • 限制或计数结果
  • 查询分组
  • 插入数据
  • 更新数据
  • 删除数据
  • 方法链
  • 查询生成器缓存
  • 重置查询生成器
  • 类引用

选择数据

以下函数允许您构建SQL选择陈述。

$this->db->获取%28%29

运行选择查询并返回结果。它可以用于检索表中的所有记录:

$query = $this->db->get('mytable');  // Produces: SELECT * FROM mytable

五参数使您能够设置限制和偏移子句:

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

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

您将注意到,上面的函数被分配给一个名为$query的变量,该变量可用于显示结果:

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

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

请访问结果函数页,以了解有关结果生成的完整讨论。

$this - > DB-> get_compiled_select()

$ this-> db-> get()一样编译选择查询,但不运行查询。该方法只是将SQL查询作为字符串返回。

例子:

$sql = $this->db->get_compiled_select('mytable');
echo $sql;

// Prints string: SELECT * FROM mytable

第二个参数使您可以设置查询构建器查询是否将被重置(默认情况下,它将被重置,就像使用时一样$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

上面例子中要注意的关键是第二个查询没有使用$ this-> db-> from(),并且没有将表名传递给第一个参数。造成这种结果的原因是因为查询并未使用$ this-> db-> get()来执行,这会重置值或直接使用$ this-> db-> reset_query()进行重置。

$这个 - > DB-> get_where()

除了可以在第二个参数中添加“where”子句,而不是使用db-> where()函数外,其他功能与上述功能相同:

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

有关更多信息,请阅读下面的WHERE函数。

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会假设您希望选择所有字段并自动添加'SELECT *'。

$this->db->select()接受可选的第二个参数。如果将其设置为false,CodeIgniter将不会试图保护您的字段名或表名。如果您需要一个复合SELECT语句,那么这是非常有用的,其中字段的自动转义可能会破坏它们。

$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)用于查询的部分。您可以选择包括第二个参数来重命名结果字段。

$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()一样,您可以选择包含第二个参数来重命名结果字段。

$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()一样,您可以选择包含第二个参数来重命名结果字段。

$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()一样,您可以选择包含第二个参数来重命名结果字段。

$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()函数中指定,因此请使用您喜欢的任何方法。

$this - > DB->join()

允许您编写查询的联接部分:

$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

如果在一个查询中需要多个联接,则可以执行多个函数调用。

如果需要特定类型的联接,可以通过函数的第三个参数指定它。选项有:左、右、外、内、左、外、右。

$this->db->join('comments', 'comments.id = blogs.id', 'left');
// Produces: LEFT JOIN comments ON comments.id = blogs.id

寻找特定数据

$this- > DB->,where()

此函数使您能够设置何地使用四种方法之一的子句:

传递给该函数的所有值都会自动转义,从而产生更安全的查询。

  • 简单的键/值方法: $ this-> db-> where('name',$ name); //产生:WHERE name ='Joe'注意等号是为你添加的。如果您使用多个函数调用,它们将与它们之间的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'
  • 自定义密钥/值方法:

您可以在第一个参数中包含一个运算符,以便控制比较:

$ this-> db-> where('name!=',$ name); $ this-> db-> where('id <',$ id); //产生:WHERE name!='Joe'AND id <45

  • 关联数组方法: $ array = array('name'=> $ name,'title'=> $ title,'status'=> $ status); $这- > DB->其中($阵列); //产生: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()接受可选的第三个参数。如果将其设置为FALSE,CodeIgniter将不会尝试保护您的字段或表名。

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

$this - > DB-> or_where()

该函数与上面的函数相同,只是多个实例由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(

此方法使您能够生成就像子句,用于搜索。

传递给此方法的所有值都会自动转义。

  1. 简单的键/值方法: $ this-> db-> like('title','match'); //产生:WHERE titleLIKE'%match%'ESCAPE'!' 如果您使用多个方法调用,它们将与它们之间的AND链接在一起:$ this-> db-> like('title','match'); $ this-> db-> like('body','match'); // WHERE titleLIKE'%match%'ESCAPE'!' AND bodyLIKE'%match%ESCAPE'!' 如果要控制放置通配符(%)的位置,可以使用可选的第三个参数。您的选项是“之前”,“之后”和“两者”(这是默认设置)。$ this-> db-> like('title','match','before'); //产生:WHERE titleLIKE'%match'ESCAPE'!' $ this-> db-> like('title',' 匹配','之后'); title//产生:WHERE LIKE'match%'ESCAPE'!' $ this-> db-> like('title','match','both'); //产生:WHEREtitle LIKE'%match%'ESCAPE'!'
  • 关联数组法:

$ array = array('title'=> $ match,'page1'=> $ match,'page2'=> $ match); $这- > DB->像($阵列); // WHERE titleLIKE'%match%'ESCAPE'!' AND page1LIKE'%match%'ESCAPE'!' AND page2LIKE'%match%'ESCAPE'!'

$this - > DB-> or_like()

此方法与上面的方法相同,只是多个实例由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(),它已经被移除了。

$this - > DB-)> not_like(

这种方法与like(),但它生成的语句不像:

$this->db->not_like('title', 'match');  // WHERE `title` NOT LIKE '%match% ESCAPE '!'

$this - > DB-> or_not_like()

这种方法与not_like(),除非多个实例由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

还可以传递多个值的数组:

$this->db->group_by(array("title", "date"));  // Produces: GROUP BY title, date

group_by()以前称为groupby(),它已被删除。

$this- > DB->distinct()

将“DISTISTY”关键字添加到查询中。

$this->db->distinct();
$this->db->get('table'); // Produces: SELECT DISTINCT * FROM table

$this - > DB->having()

允许您编写查询的有部分。有两个可能的语法,一个论点或两个:

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

还可以传递多个值的数组:

$this->db->having(array('title =' => 'My Title', 'id <' => $id));
// Produces: HAVING title = 'My Title', id < 45

如果使用CodeIgniter转义查询的数据库,则可以通过传递可选的第三个参数并将其设置为false来防止转义内容。

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

允许您设置ORDERBY子句。

第一个参数包含要按其排序的列的名称。

第二个参数可让您设置结果的方向。选项是ASCDESCRANDOM

$this->db->order_by('title', 'DESC');
// Produces: ORDER BY `title` DESC

您还可以在第一个参数中传递您自己的字符串:

$this->db->order_by('title DESC, name ASC');
// Produces: ORDER BY `title` DESC, `name` ASC

或者,如果需要多个字段,则可以进行多个函数调用。

$this->db->order_by('title', 'DESC');
$this->db->order_by('name', 'ASC');
// Produces: ORDER BY `title` DESC, `name` ASC

如果您选择随机选项,则第一个参数将被忽略,除非指定一个数值种子值。

$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目前不支持随机排序,而是默认为ASC。

限制或计数结果

$this->db->limit()

允许您限制希望由查询返回的行数:

$this->db->limit(10);  // Produces: LIMIT 10

第二个参数允许您设置结果偏移量。

$this->db->limit(10, 20);  // Produces: LIMIT 20, 10 (in MySQL.  Other databases have slightly different syntax)

$this->db->count_all_results()

允许您确定特定活动记录查询中的行数。查询将接受查询生成器限制项,如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作为第二个参数:

echo $this->db->count_all_results('my_table', FALSE);

$this->db->count_all()

允许您确定特定表中的行数。在第一个参数中提交表名。例子:

echo $this->db->count_all('my_table');  // Produces an integer, like 25

查询分组

查询分组允许您通过将WHERE子句括在括号中来创建WHERE子句组。这将允许您创建具有复杂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子句添加一个结束括号来结束当前组。

插入数据

$this->db->insert()

根据所提供的数据生成插入字符串,并运行查询。您可以通过一个列阵或者对象为了这个功能。下面是一个使用数组的示例:

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

第一个参数将包含表名,第二个参数是值的关联数组。

下面是一个使用对象的示例:

/*
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')

第一个参数将包含表名,第二个参数将包含一个对象。

所有值都会自动转义,从而产生更安全的查询。

$this->db->get_compiled_insert()

像$ this-> db-> insert()一样编译插入查询,但不运行查询。该方法只是将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')

第二个参数使您可以设置查询构建器查询是否将被重置(默认情况下它将与$ 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')

在上面的例子中需要注意的关键是第二个查询没有使用$this->db->from()它也没有将表名传递给第一个参数。此操作之所以有效,是因为没有使用$this->db->insert()使用以下方法重置值或直接重置$this->db->reset_query()...

此方法不适用于批处理插入。

$this - > DB-> insert_batch()

根据所提供的数据生成插入字符串,并运行查询。您可以通过一个列阵或者对象为了这个功能。下面是一个使用数组的示例:

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

第一个参数将包含表名,第二个参数是值的关联数组。

所有值都会自动转义,从而产生更安全的查询。

更新数据

$this->db->replace()

此方法执行REPLACE语句,该语句基本上是(可选的)DELETE + INSERT的SQL标准,使用PRIMARYUNIQUE键作为决定性因素。在我们的例子中,它可以使你免于需要实现与不同的组合复杂的逻辑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')

在上面的示例中,如果我们假设标题字段是我们的主键,那么如果一行包含“我的标题”作为标题值时,将删除该行,并替换新的行数据。

使用set()方法,并且所有字段都会自动转义,如下所示insert()...

$this->db->set()

此函数使您能够为插入或更新设置值。

可以使用它而不是直接将数据数组传递给INSERT或UPDATE函数:

$this->db->set('name', $name);
$this->db->insert('mytable');  // Produces: INSERT INTO mytable (`name`) VALUES ('{$name}')

如果您使用多个调用的函数,则将根据您正在执行的插入或更新正确地组装它们:

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

set()也会接受一个可选的第三个参数($escape),如果设置为FALSE,这将防止数据被转义。为了说明不同之处,这里set()使用和不使用escape参数。

$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

还可以将关联数组传递给此函数:

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

生成更新字符串,并根据所提供的数据运行查询。你可以通过一个列阵或者对象为了这个功能。下面是一个使用数组的示例:

$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`

所有值都会自动转义,从而产生更安全的查询。

您会注意到使用$ this-> db-> where()函数,使您可以设置WHERE子句。您可以选择将此信息直接作为字符串传递给更新函数:

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

或者作为一个数组:

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

执行更新时,您也可以使用上述的$ this-> db-> set()函数。

$this - > DB-> update_batch()

根据所提供的数据生成更新字符串,并运行查询。您可以通过一个列阵或者对象为了这个功能。下面是一个使用数组的示例:

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

第一个参数将包含表名,第二个参数是值的关联数组,第三个参数是WHERE键。

所有值都会自动转义,从而产生更安全的查询。

affected_rows()由于它的工作原理,它不会给出正确的结果。相反,update_batch()返回受影响的行数。

$this->db->get_compiled_update()

它的工作方式与$this->db->get_compiled_insert()但是,它生成一个更新SQL字符串,而不是插入SQL字符串。

此方法不适用于批处理更新。

删除数据

$this->db->delete()

生成一个DELETE SQL字符串并运行查询。

$this->db->delete('mytable', array('id' => $id));  // Produces: // DELETE FROM mytable  // WHERE id = $id

第一个参数是表名,第二个参数是where子句。您也可以使用where()或or_where()函数,而不是将数据传递给函数的第二个参数:

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

// Produces:
// DELETE FROM mytable
// WHERE id = $id

如果要从多个表中删除数据,可以将一个表名数组传递给delete()。

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

如果要删除表中的所有数据,可以使用truncate()函数或empty_table()。

$this->db->empty_table()

生成删除SQL字符串并运行查询。

$this->db->empty_table('mytable'); // Produces: DELETE FROM mytable

$this->db->truncate()

生成截断SQL字符串并运行查询。

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

// or

$this->db->truncate('mytable');

// Produce:
// TRUNCATE mytable

如果TRUNCATE命令不可用,truncate()将作为“DELETE FROM table”执行。

$this->db->get_compiled_delete()

它的工作方式与$this->db->get_compiled_insert()但是,它生成一个DELETE SQL字符串,而不是INSERT SQL字符串。

有关更多信息,请查看$ this-> db-> get_compiled_insert()的文档。

方法链

方法链接允许您通过连接多个函数来简化语法。考虑这个例子:

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

查询生成器缓存

虽然不是“真正的”缓存,但查询生成器使您可以保存(或“缓存”)查询的某些部分,以便稍后在脚本执行过程中重新使用。通常情况下,当查询构建器调用完成时,所有存储的信息都会重置以用于下一次调用。通过缓存,您可以防止此重置,并轻松地重用信息。

缓存的呼叫是累积的。如果进行2次缓存的select()调用,然后进行2次未缓存的select()调用,则会导致4次select()调用。有三种缓存功能可用:

$this->db->start_cache()

必须调用此函数才能开始缓存。所有正确类型的查询生成器查询(请参阅下面的支持的查询)都存储起来供以后使用。

$this->db->stop_cache()

可以调用此函数来停止缓存。

$this->db->flush_cache()

此函数从查询生成器缓存中删除所有项。

缓存示例

下面是一个用法示例:

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

重置查询生成器允许你重新启动你的查询,而不用先执行它,像$ this-> db-> get()或$ this-> db-> insert()。就像执行查询的方法一样,这不会重置使用查询生成器缓存进行缓存的项目。

这在您使用查询生成器生成SQL(例如$this->db->get_compiled_select())但是然后选择运行查询的情况下非常有用:

// 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()当您使用QueryBuilder缓存功能而不重置查询时,将导致缓存合并两次。反过来,如果你在缓存一个select()-两次选择同一个字段。

类引用

class CI_DB_query_builderreset_query()

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

start_cache()

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

stop_cache()

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

flush_cache()

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

set_dbprefix([$prefix = ''])

参数:

$ prefix(string) - 要使用的新前缀

返回:

数据库前缀正在使用中

返回类型:

  • $ prefixstring) - 要使用的新前缀返回:正在使用的数据库前缀返回类型:字符串设置数据库前缀,而不必重新连接。dbprefix([$table = ''])参数:$ table(string) - 表名称prefixReturns:前缀表名返回类型:字符串
  • $ tablestring) - 表格名称作为前缀
Returns:  The prefixed table name
Return type:  string
Prepends a database prefix, if one exists in configuration.

count_all_results([$table = ''[, $reset = TRUE]])

参数:

$ table(string) - 表名$ reset(bool) - 是否重置SELECTs的值

返回:

查询结果中的行数

返回类型:

INT

  • $ tablestring) - 表名
  • $ resetbool) - 是否重置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]]])

参数:

$ table(string) - 要查询的表$ limit(int) - LIMIT子句$ offset(int) - OFFSET子句

返回:

CI_DB_result实例(方法链接)

返回类型:

CI_DB_result

  • $ tablestring) - 要查询的表
  • $ limitint) - LIMIT子句
  • $ offsetint) - 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]]]])

参数:

$ table(mixed) - 从中​​获取数据的表格; 字符串或数组$ where(string) - WHERE子句$ limit(int) - LIMIT子句$ offset(int) - OFFSET子句

返回:

CI_DB_result实例(方法链接)

返回类型:

CI_DB_result

  • $ tablemixed) - 从中​​获取数据的表格; 字符串或数组
  • $ wherestring) - WHERE子句
  • $ limitint) - LIMIT子句
  • $ offsetint) - 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]])

参数:

$ select(string) - 查询的SELECT部分​​$ escape(bool) - 是否转义值和标识符

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ selectstring) - 查询的SELECT部分
  • $ escapebool) - 是否转义值和标识符
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 = '']])

参数:

$ select(string) - 用于计算$ alias(字符串)的平均值的字段 - 结果值名称的别名

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ selectstring) - Field来计算平均值
  • $ aliasstring) - 结果值名称的别名
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 = '']])

参数:

$ select(string) - 用于计算$ alias(字符串)的最大值的字段 - 结果值名称的别名

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ selectstring) - Field来计算最大值
  • $ aliasstring) - 结果值名称的别名
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 = '']])

参数:

$ select(string) - 用于计算$ alias(字符串)的最小值的字段 - 结果值名称的别名

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ selectstring) - Field来计算最小值
  • $ aliasstring) - 结果值名称的别名
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 = '']])

参数:

$ select(string) - 用于计算$ alias(字符串)的总和的字段 - 结果值名称的别名

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ selectstring) - Field来计算总和
  • $ aliasstring) - 结果值名称的别名
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])

参数:

$ val(bool) - “distinct”标志的期望值

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ valbool) - “distinct”标志的期望值返回:CI_DB_query_builder实例(方法链)返回类型:CI_DB_query_builder设置一个标志,通知查询构建器将DISTINCT子句添加到查询的SELECT部分​​。from($from)参数:$ from(mixed) - 表名(s); 字符串或数组返回:CI_DB_query_builder实例(方法链)返回类型:CI_DB_query_builder
  • $ frommixed) - 表名(s); 字符串或数组
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]])

参数:

$ table(string) - 表名加入$ cond(string) - JOIN ON条件$ type(string) - JOIN类型$ escape(bool) - 是否转义值和标识符

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ tablestring) - 表名加入
  • $ condstring) - JOIN ON条件
  • $ typestring) - JOIN类型
  • $ escapebool) - 是否转义值和标识符
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]])

参数:

$ key(mixed) - 要比较的字段的名称或关联数组$ value(mixed) - 如果与此值进行比较的单个键$ escape(bool) - 是否要转义值和标识符

返回:

DB_query_builder实例

返回类型:

目的

  • $ keymixed) - 要比较的字段的名称或关联数组
  • $ value混合) - 如果单个键与此值相比较
  • $ escapebool) - 是否转义值和标识符
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]])

参数:

$ key(mixed) - 要比较的字段的名称或关联数组$ value(mixed) - 如果与此值进行比较的单个键$ escape(bool) - 是否要转义值和标识符

返回:

DB_query_builder实例

返回类型:

目的

  • $ keymixed) - 要比较的字段的名称或关联数组
  • $ value混合) - 如果单个键与此值相比较
  • $ escapebool) - 是否转义值和标识符
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]]])

参数:

$ key(string) - 要搜索的字段$ values(array) - 在$ escape(bool)上搜索的值 - 是否要转义值和标识符

返回:

DB_query_builder实例

返回类型:

object

  • $ keystring) - 要搜索的字段
  • $ valuesarray) - 搜索的值
  • $ escapebool) - 是否转义值和标识符
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]]])

参数:

$ key(string) - 要搜索的字段$ values(array) - 在$ escape(bool)上搜索的值 - 是否要转义值和标识符

返回:

DB_query_builder实例

返回类型:

目的

  • $ keystring) - 要搜索的字段
  • $ valuesarray) - 搜索的值
  • $ escapebool) - 是否转义值和标识符
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]]])

参数:

$ key(string) - 要检查的字段名称values(array) - 目标值数组$ escape(bool) - 是否要转义值和标识符

返回:

DB_query_builder实例

返回类型:

目的

  • $ keystring) - 要检查的字段的名称
  • $ valuesarray) - 目标值数组
  • $ escapebool) - 是否转义值和标识符
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]]])

参数:

$ key(string) - 要检查的字段名称values(array) - 目标值数组$ escape(bool) - 是否要转义值和标识符

返回:

DB_query_builder实例

返回类型:

目的

  • $ keystring) - 要检查的字段的名称
  • $ valuesarray) - 目标值数组
  • $ escapebool) - 是否转义值和标识符
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实例(方法链)

返回类型:

CI_DB_query_builder

or_group_start()

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

not_group_start()

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

or_not_group_start()

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

group_end()

返回:

DB_query_builder实例

返回类型:

目的

like($field[, $match = ''[, $side = 'both'[, $escape = NULL]]])

参数:

$ field(string) - 字段名称$ match(字符串) - 文本部分匹配$ side(字符串) - 表达式的哪一边将'%'通配符放在$ escape(bool)上 - 是否要转义值和标识符

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ fieldstring) - 字段名称
  • $ matchstring) - 要匹配的文本部分
  • $ sidestring) - 表达式的哪一边放置'%'通配符
  • $ escapebool) - 是否转义值和标识符
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]]])

参数:

$ field(string) - 字段名称$ match(字符串) - 文本部分匹配$ side(字符串) - 表达式的哪一边将'%'通配符放在$ escape(bool)上 - 是否要转义值和标识符

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ fieldstring) - 字段名称
  • $ matchstring) - 要匹配的文本部分
  • $ sidestring) - 表达式的哪一边放置'%'通配符
  • $ escapebool) - 是否转义值和标识符
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]]])

参数:

$ field(string) - 字段名称$ match(字符串) - 文本部分匹配$ side(字符串) - 表达式的哪一边将'%'通配符放在$ escape(bool)上 - 是否要转义值和标识符

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ fieldstring) - 字段名称
  • $ matchstring) - 要匹配的文本部分
  • $ sidestring) - 表达式的哪一边放置'%'通配符
  • $ escapebool) - 是否转义值和标识符
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]]])

参数:

$ field(string) - 字段名称$ match(字符串) - 文本部分匹配$ side(字符串) - 表达式的哪一边将'%'通配符放在$ escape(bool)上 - 是否要转义值和标识符

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ fieldstring) - 字段名称
  • $ matchstring) - 要匹配的文本部分
  • $ sidestring) - 表达式的哪一边放置'%'通配符
  • $ escapebool) - 是否转义值和标识符
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]])

参数:

$ key(字符串) - 如果$ key是标识符,则寻找的值$ escape(string) - 是否要转义值和标识符

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ key混合) - 字段/值对的标识符(字符串)或关联数组
  • $ valuestring) - 如果$ key是标识符,则查找值
  • $ escapestring) - 是否转义值和标识符
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]])

参数:

$ key(字符串) - 如果$ key是标识符,则寻找的值$ escape(string) - 是否要转义值和标识符

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ key混合) - 字段/值对的标识符(字符串)或关联数组
  • $ valuestring) - 如果$ key是标识符,则查找值
  • $ escapestring) - 是否转义值和标识符
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])

参数:

$ by(mixed) - Field to(s)to group by; 字符串或数组

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ bymixed) - Field to(s)to group by; 字符串或数组返回:CI_DB_query_builder实例(方法链)返回类型:CI_DB_query_builder向查询添加GROUP BY子句。order_by($orderby[, $direction = ''[, $escape = NULL]])参数:$ orderby(string) - 按照$ direction排序的字段(字符串) - 请求的顺序 - ASC,DESC或随机$ escape(bool) - 是否转义值和标识符返回:CI_DB_query_builder实例(方法链)返回类型:CI_DB_query_builder
  • $ orderbystring) - 要排序的字段
  • $方向字符串) - 请求的顺序 - ASC,DESC或随机
  • $ escapebool) - 是否转义值和标识符
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])

参数:

$ value(int) - 将结果限制到$ offset的行数(int) - 要跳过的行数

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ valueint) - 将结果限制到的行数
  • $ offsetint) - 要跳过的行数
Returns:  CI\_DB\_query\_builder instance (method chaining)
Return type:  CI\_DB\_query\_builder
Adds LIMIT and OFFSET clauses to a query.

offset($offset)

参数:

$ offset(int) - 要跳过的行数

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ offsetint) - 要跳过的行数返回:CI_DB_query_builder实例(方法链)返回类型:CI_DB_query_builder向查询添加OFFSET子句。set($key[, $value = ''[, $escape = NULL]])参数:$ key(mixed) - 字段名称或字段/值对的数组$ value(string) - 字段值,如果$ key是单个字段$ escape(bool) - 是否转义值和标识符返回:CI_DB_query_builder实例(方法链)返回类型:CI_DB_query_builder
  • $ keymixed) - 字段名称或字段/值对的数组
  • $ value字符串) - 字段值,如果$ key是单个字段
  • $ escapebool) - 是否转义值和标识符
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]]])

参数:

$ table(string) - 表名$ set(array) - 一个字段/值对的关联数组$ escape(bool) - 是否要转义值和标识符

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ tablestring) - 表名
  • $ setarray) - 一个字段/值对的关联数组
  • $ escapebool) - 是否转义值和标识符
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]]])

参数:

$ table(string) - 表名$ set(array) - 要插入的数据$ escape(bool) - 是否要转义值和标识符$ batch_size(int) - 要一次插入的行数

返回:

插入的行数或失败时的FALSE

返回类型:

mixed

  • $ tablestring) - 表名
  • $ setarray) - 要插入的数据
  • $ escapebool) - 是否转义值和标识符
  • $ batch_sizeint) - 一次插入的行数
Returns:  Number of rows inserted or FALSE on failure
Return type:  mixed
Compiles and executes batch `INSERT` statements.

当超过$batch_size提供多个行INSERT查询将被执行,每个查询都试图插入$batch_size一排排。

set_insert_batch($key[, $value = ''[, $escape = NULL]])

参数:

$ key(mixed) - 字段名称或字段/值对数组$ value(string) - 字段值,如果$ key是单个字段$ escape(bool) - 是否要转义值和标识符

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ keymixed) - 字段名称或字段/值对的数组
  • $ value字符串) - 字段值,如果$ key是单个字段
  • $ escapebool) - 是否转义值和标识符
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]]]])

参数:

$ table(string) - 表名$ set(array) - 一个字段/值对的关联数组$ where(string) - WHERE子句$ limit(int) - LIMIT子句

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ tablestring) - 表名
  • $ setarray) - 一个字段/值对的关联数组
  • $ wherestring) - WHERE子句
  • $ limitint) - 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]]])

参数:

$ table(string) - 表名$ set(array) - 字段名或字段/值对的关联数组$ value(string) - 字段值,如果$ set是单个字段$ batch_size(int) - 条件以在单个查询中分组

返回:

更新的行数或失败时的FALSE

返回类型:

  • $ tablestring) - 表名
  • $ setarray) - 字段名称或字段/值对的关联数组
  • $ value字符串) - 字段值,如果$ set是单个字段
  • $ batch_sizeint) - 要在单个查询中分组的条件数
Returns:  Number of rows updated or FALSE on failure
Return type:  mixed
Compiles and executes batch `UPDATE` statements.

当超过$batch_size提供了字段/值对,将执行多个查询,每次处理$batch_size字段/值对。

set_update_batch($key[, $value = ''[, $escape = NULL]])

参数:

$ key(mixed) - 字段名称或字段/值对数组$ value(string) - 字段值,如果$ key是单个字段$ escape(bool) - 是否要转义值和标识符

返回:

CI_DB_query_builder实例(方法链)

返回类型:

CI_DB_query_builder

  • $ keymixed) - 字段名称或字段/值对的数组
  • $ value字符串) - 字段值,如果$ key是单个字段
  • $ escapebool) - 是否转义值和标识符
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]])

参数:

$ table(string) - 表名$ set(array) - 一个字段/值对的关联数组

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ tablestring) - 表名
  • $ setarray) - 一个字段/值对的关联数组
Returns:  TRUE on success, FALSE on failure
Return type:  bool
Compiles and executes a REPLACE statement.

delete([$table = ''[, $where = ''[, $limit = NULL[, $reset_data = TRUE]]]])

参数:

$ table(mixed) - 从中​​删除的表格; 字符串或数组$ where(string) - WHERE子句$ limit(int) - LIMIT子句$ reset_data(bool) - TRUE重置查询“写入”子句

返回:

CI_DB_query_builder实例(方法链)或失败时为FALSE

返回类型:

  • $ tablemixed) - 从中​​删除的表格; 字符串或数组
  • $ wherestring) - WHERE子句
  • $ limitint) - LIMIT子句
  • $ reset_databool) - TRUE重置查询“写入”子句
Returns:  CI\_DB\_query\_builder instance (method chaining) or FALSE on failure
Return type:  mixed
Compiles and executes a DELETE query.

truncate([$table = ''])

参数:

$ table(string) - 表名

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ tablestring) - 表名返回:成功时为TRUE,失败时为FALSE返回类型:BOOL在表上执行TRUNCATE语句。注意如果正在使用的数据库平台不支持TRUNCATE,则将使用DELETE语句。empty_table([$table = ''])参数:$ table(string) - 表名返回:成功时为TRUE,失败时返回FALSE返回类型:bool
  • $ tablestring) - 表名
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]])

参数:

$ table(string) - 表名$ reset(bool) - 是否重置当前QB值

返回:

编译后的SQL语句为一个字符串

返回类型:

  • $ tablestring) - 表名
  • $ resetbool) - 是否重置当前的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]])

参数:

$ table(string) - 表名$ reset(bool) - 是否重置当前QB值

返回:

编译后的SQL语句为一个字符串

返回类型:

  • $ tablestring) - 表名
  • $ resetbool) - 是否重置当前的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]])

参数:

$ table(string) - 表名$ reset(bool) - 是否重置当前QB值

返回:

编译后的SQL语句为一个字符串

返回类型:

  • $ tablestring) - 表名
  • $ resetbool) - 是否重置当前的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]])

参数:

$ table(string) - 表名$ reset(bool) - 是否重置当前QB值

返回:

编译后的SQL语句为一个字符串

返回类型:

  • $ tablestring) - 表名
  • $ resetbool) - 是否重置当前的QB值
返回:编译后的SQL语句作为字符串
Return type:  string
编译DELETE语句并将其作为字符串返回。
Codeigniter 3

CodeIgniter 是一个PHP MVC框架,特点是超轻量级、有数据加密、有灵活URI路由等。对于 PHP 程序员来说,它小巧但功能强大。

主页 https://codeigniter.com/
源码 https://github.com/bcit-ci/CodeIgniter
版本 3
发布版本 3.1.5