CodeIgniter学习笔记 Item5--CI中的AR_count_all_results gorup_by

$this->db->get_where();

跟上面的函数一样,只是它允许你在函数的第二个参数那里添加一个 where 从句,从而不用使用 db->where() 这个函数:

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

注意: get_where() 在以前的版本中写作 getwhere(),这是已经过时的用法,现已从代码中移除 getwhere()。

$this->db->select();

允许你在SQL查询中写 SELECT 部分:

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

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

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

注意: 如果你要查询表中的所有行,你可以不用写这个函数。省略后,CodeIgniter 会认为你要查询全部行(SELECT *)。

$this->db->select()可接受一个可选的第二个参数。如果你把它设为FALSE, CodeIgniter 将不会使用反引号保护你的字段或者表名。这在进行复合查询时很有用。

$this->db->select('(SELECT SUM(payments.amount) FROM payments WHERE payments.invoice\_id=4') AS amount\_paid', FALSE); 
$query = $this->db->get('mytable');

关联数组方式:

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

本函数与上面那个函数几乎完全相同,唯一的区别是多个实例之间是用 OR 连接起来的:

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

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

说明: or_like() 曾经被称为 orlike(), 后者已经过时,现已从代码中移除 orlike()。

$this->db->not_like();

本函数与 like() 几乎完全相同,唯一的区别是它生成 NOT LIKE 语句:

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

// WHERE title NOT LIKE '%match%

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

$this->db->group_by();

允许你编写查询语句中的 GROUP BY 部分:

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

// 生成: GROUP BY title 

你也可以把多个值作为数组传递过去:

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

// 生成: GROUP BY title, date

说明: group_by() 曾经被称为 groupby(), 后者已经过时,现已从代码中移除 groupby()。

$this->db->distinct();

为查询语句添加 “DISTINCT” 关键字:

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

// 生成: SELECT DISTINCT \* FROM table

$this->db->having();

允许你为你的查询语句编写 HAVING 部分。有两种语法形式,一个或两个参数都可以:

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

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

你也可以把多个值通过数组传递过去:

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

// 生成: HAVING title = 'My Title', id < 45

如果你正在使用一个由CodeIgniter进行转义保护的数据库,为了避免内容转义,你可以传递可选的第三个参数,并将其设置为FALSE。

$this->db->having('user\_id', 45); 
// 生成: HAVING `user\_id` = 45 (在诸如MySQL等数据库中) 
$this->db->having('user\_id', 45, FALSE); 
// 生成: HAVING user\_id = 45

$this->db->or_having();

与 having() 函数几乎完全一样,唯一的区别是多个子句之间是用 “OR” 分隔的。

$this->db->order_by();

帮助你设置一个 ORDER BY 子句。第一个参数是你想要排序的字段名。第二个参数设置结果的顺序,可用的选项包括 asc (升序)或 desc(降序), 或 random(随机)。

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

// 生成: ORDER BY title DESC 

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

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

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

或者,多次调用本函数就可以排序多个字段。

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

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

说明: order_by() 曾经被称为 orderby(), 后者已经过时,现已从代码中移除 orderby()。

说明: 目前 Oracle 和 MSSQL 的驱动还不支持随机排序,将被默认设置为 ‘ASC’(升序)。

$this->db->limit();

限制查询所返回的结果数量:

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

// 生成: LIMIT 10

第二个参数设置的是结果偏移量。

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

// 生成: LIMIT 20, 10 (仅限MySQL中。其它数据库有稍微不同的语法)

$this->db->count_all_results();

允许你获得某个特定的Active Record查询所返回的结果数量。可以使用Active Record限制函数,例如 where(), or_where(), like(), or_like() 等等。范例:

echo $this->db->count_all_results('my\_table');
// 生成一个整数,例如 25

$this->db->like('title', 'match');
$this->db->from('my\_table');
echo $this->db->count_all_results();
// 生成一个整数,例如 17 

插入数据

$this->db->insert();

生成一条基于你所提供的数据的SQL插入字符串并执行查询。你可以向函数传递 数组 或一个 对象。下面是一个使用数组的例子:

$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 {
        var $title = 'My Title';
        var $content = 'My Content';
        var $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')

第一个参数包含表名,第二个是一个对象。(原文有错:The first parameter will contain the table name, the second is an associative array of values.)

注意: 所有的值已经被自动转换为安全查询。

$this->db->set();

本函数使您能够设置inserts(插入)或updates(更新)值。

它可以用来代替那种直接传递数组给插入和更新函数的方式:

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

// 生成: 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() 函数做一个说明。

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

你也可以将一个关联数组传递给本函数:

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

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

或者一个对象也可以:

/*
    class Myclass {
        var $title = 'My Title';
        var $content = 'My Content';
        var $date = 'My Date';
    }
*/

$object = new Myclass;

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

更新数据

$this->db->update();

根据你提供的数据生成并执行一条update(更新)语句。你可以将一个数组或者对象传递给本函数。这里是一个使用数组的例子:

$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

或者你也可以传递一个对象:

/\*
 class Myclass {
 var $title = 'My Title';
 var $content = 'My Content';
 var $date = 'My Date';
 }
\*/

$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

说明: 所有值都会被自动转义,以便生成安全的查询。

你会注意到 $this->db->where() 函数的用法,它允许你设置 WHERE 子句。你可以有选择性地将这一信息直接以字符串的形式传递给 update 函数:

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

或者是一个数组:

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

在进行更新时,你还可以使用上面所描述的 $this->db->set() 函数。

删除数据

$this->db->delete();

生成并执行一条DELETE(删除)语句。

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

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

第一个参数是表名,第二个参数是where子句。你可以不传递第二个参数,使用 where() 或者 or_where() 函数来替代它:

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

// 生成:
// 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();

生成并执行一条DELETE(删除)语句。

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

// 生成
// DELETE FROM mytable

$this->db->truncate();

生成并执行一条TRUNCATE(截断)语句。

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

// 生成:
// TRUNCATE mytable 

说明: 如果 TRUNCATE 命令不可用,truncate() 将会以 “DELETE FROM table” 的方式执行。

链式方法
链式方法允许你以连接多个函数的方式简化你的语法。考虑一下这个范例:

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

### 自学几个月前端,为什么感觉什么都没学到??

----------------------------------------------------------------------------------

这种现象在很多的初学者和自学前端的同学中是比较的常见的。

因为自学走的弯路是比较的多的,会踩很多的坑,学习的过程中是比较的迷茫的。

最重要的是,在学习的过程中,不知道每个部分该学哪些知识点,学到什么程度才算好,学了能做什么。

很多自学的朋友往往都是自己去找资料学习的,资料上有的或许就学到了,资料上没有的或许就没有学到。



这就会给人一个错误的信息就是,我把资料上的学完了,估计也-就差不多的了。

但是真的是这样的吗?非也,因为很多人找的资料就是很基础的。学完了也就是掌握一点基础的东西。分享给你一份前端分析路线,你可以参考。



![](https://img-blog.csdnimg.cn/img_convert/15be8206a9f6e5bd9e8e930303b613ee.png)



还有很多的同学在学习的过程中一味的追求学的速度,很快速的刷视频,写了后面忘了前面,最后什么都没有学到,什么都知道,但是什么都不懂,要具体说,也说不出个所以然。



所以学习编程一定要注重实践操作,练习敲代码的时间一定要多余看视频的时间。
  • 23
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值