Active Record简化数据库操作
1、编辑system/application/config/database.php配置文件,将下列参数填好。
$active_group = "default";
$db['default']['hostname'] = "";
$db['default']['username'] = "";
$db['default']['password'] = "";
$db['default']['database'] = "";
$db['default']['dbdriver'] = "";
说明:
hostname: 你的数据库的位置, 举例来说, 'localhost' 或 IP 地址
username和password: 使用者名称和密码必须有充分的权限,允许你的网站存取数据库中的数据。
database: 你的数据库的名字, 举例来说, 'websits'
dbdriver: 你正在使用的数据库的类型 - CI可受的有选项有MySQL、MySQLi、 Postgre SQL、ODBC和MS SQL
2、数据库操作
AR是一个“设计模式”。它的核心是把你的数据库和PHP对象建立一个对应关系。每次当你执行一个QUERY语句。每一张数据库里的表是一个类,每一行是一个对象。所有你需要做的只是创建它,修改它或者删除它。
(1)连接数据库
$this->load->database();
(2)执行查询
$query = $this->db->get('sites'); //select * From sites
$this->db->select('url','name','clientid'); // Select url,name,clientid from sites order by name desc
$this->db->orderby("name", "desc");
$query = $this->db->get('sites');
$this->db->select('url','name','clientid','people.surname AS client');
$this->db->where('clientid', '3');
$this->db->limit(5);
$this->db->from('sites');
$this->db->join('people', 'sites.clientid = people.id');
$this->db->orderby("name", "desc");
$query = $this->db->get();
// 循环显示
foreach ($query->result() as $row)
{
print $row->url;
print $row->name;
print $row->client;
}
(3)添加新记录
方法1:
$data = array(
'url' => 'www.mynewclient.com',
'name' => 'BigCo Inc',
'clientid' => '33',
'type' => 'dynamic'
);
$this->db->insert('sites', $data);
方法2:
$this->db->set('url', 'www.mynewclinet.com');
$this->db->set('name', 'BigCo Inc');
$this->db->set('clientid', '33');
$this->db->set('type', 'dynamic');
$this->db->insert('sites');
(3)更新记录
$this->db->where('id', '1');
$this->db->update('sites', $data);
(4)删除记录
$this->db->where('id', '2');
$this->db->delete('sites');
$active_group = "default";
$db['default']['hostname'] = "";
$db['default']['username'] = "";
$db['default']['password'] = "";
$db['default']['database'] = "";
$db['default']['dbdriver'] = "";
说明:
hostname: 你的数据库的位置, 举例来说, 'localhost' 或 IP 地址
username和password: 使用者名称和密码必须有充分的权限,允许你的网站存取数据库中的数据。
database: 你的数据库的名字, 举例来说, 'websits'
dbdriver: 你正在使用的数据库的类型 - CI可受的有选项有MySQL、MySQLi、 Postgre SQL、ODBC和MS SQL
2、数据库操作
AR是一个“设计模式”。它的核心是把你的数据库和PHP对象建立一个对应关系。每次当你执行一个QUERY语句。每一张数据库里的表是一个类,每一行是一个对象。所有你需要做的只是创建它,修改它或者删除它。
(1)连接数据库
$this->load->database();
(2)执行查询
$query = $this->db->get('sites'); //select * From sites
$this->db->select('url','name','clientid'); // Select url,name,clientid from sites order by name desc
$this->db->orderby("name", "desc");
$query = $this->db->get('sites');
$this->db->select('url','name','clientid','people.surname AS client');
$this->db->where('clientid', '3');
$this->db->limit(5);
$this->db->from('sites');
$this->db->join('people', 'sites.clientid = people.id');
$this->db->orderby("name", "desc");
$query = $this->db->get();
// 循环显示
foreach ($query->result() as $row)
{
print $row->url;
print $row->name;
print $row->client;
}
(3)添加新记录
方法1:
$data = array(
'url' => 'www.mynewclient.com',
'name' => 'BigCo Inc',
'clientid' => '33',
'type' => 'dynamic'
);
$this->db->insert('sites', $data);
方法2:
$this->db->set('url', 'www.mynewclinet.com');
$this->db->set('name', 'BigCo Inc');
$this->db->set('clientid', '33');
$this->db->set('type', 'dynamic');
$this->db->insert('sites');
(3)更新记录
$this->db->where('id', '1');
$this->db->update('sites', $data);
(4)删除记录
$this->db->where('id', '2');
$this->db->delete('sites');