background image

php 之 CodeIgniter 学习

在使用数据库之前,我们最好将数据库进行自动连接:config/autoload.php 自动加载 
$autoload['libraries'] = array('database');

一些常用函数

选择数据

$this->db->select();

允许你在 SQL

 

查询中写 SELECT 部分。

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

允许你在 SQL

 

查询中写 WHERE 部分,其余各种 where 语句请看手册。

$this->db->get();

运行选择查询语句并且返回结果集。可以获取一个表的全部数据。

$this->db->like();
$this->db->or_like();
$this->db->not_like();

 

本函数允许你生成 LIKE 子句,在做查询时非常有用,其余语法请看手册。

$this->db->order_by();

 

帮助你设置一个 ORDER BY 子句。

$this->db->group_by();

 

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

$this->db->distinct();

 

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

$this->db->having();

 

允许你为你的查询语句编写 HAVING 部分。

$this->db->limit();

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

$this->db->select_max();

 

为你的查询编写一个 "SELECT MAX(field)"。

$this->db->select_min();

 

为你的查询编写一个 "SELECT MIN(field)" 。

$this->db->select_avg();

 

为你的查询编写一个 "SELECT AVG(field)" 。

$this->db->select_sum();

 

为你的查询编写一个 "SELECT SUM(field)" 。

$this->db->join();

允许你编写查询中的 JOIN 部分。

$this->db->count_all_results();

允许你获得某个特定的 Active Record 查询所返回的结果数量。可以使用 Active Record 限制

 

函数,例如 where(), or_where(), like(), or_like() 等等。