首页 > 代码库 > CI框架的数据库操作函数
CI框架的数据库操作函数
//举例查询 (userid,name,sex为user表的数据库字段)
public function testa(){
$this->load->database();
$query = $this->db->query(‘SELECT * FROM user‘);
foreach ($query->result() as $row)
{
echo $row->userid;
echo $row->name;
echo $row->sex;
}
}
//举例插入(userid,name,sex为user表的数据库字段)
public function testb(){
$this->load->database();
$data = http://www.mamicode.com/array(‘userid‘ => ‘22‘,‘name‘ => ‘lisi‘, ‘sex‘ => ‘1‘);
echo $str = $this->db->insert(‘user‘, $data);
}
//举例更新(userid,name,sex为user表的数据库字段)
public function testc(){
$this->load->database();
$data = http://www.mamicode.com/array(‘userid‘ => ‘22‘,‘name‘ => ‘lisi‘, ‘sex‘ => ‘1‘);
$this ->db->where(‘userid‘,1);
$this->db->update(‘user‘, $data);
}
//举例删除(userid,name,sex 为)
public function testd(){
$this->load->database();
$data = http://www.mamicode.com/array(‘userid‘ => ‘22‘,‘name‘ => ‘lisi‘, ‘sex‘ => ‘1‘);
$this ->db->where(‘userid‘,22);
$this->db->delete(‘user‘, $data);
}
//CI视图加载显示变量
public function index()
{
$this->load->library(‘parser‘);
$data = http://www.mamicode.com/array(
‘blog_title‘ => ‘My Blog Title‘,
‘blog_heading‘ => ‘My Blog Heading‘,
‘blog_entries‘ => array(
array(‘title‘ => ‘Title 1‘, ‘body‘ => ‘Body 1‘),
array(‘title‘ => ‘Title 2‘, ‘body‘ => ‘Body 2‘),
array(‘title‘ => ‘Title 3‘, ‘body‘ => ‘Body 3‘),
array(‘title‘ => ‘Title 4‘, ‘body‘ => ‘Body 4‘),
array(‘title‘ => ‘Title 5‘, ‘body‘ => ‘Body 5‘)
)
);
$this->parser->parse(‘welcome_message‘, $data);
$this->load->view(‘welcome_message‘);
}
welcome_message.php
<html>
<head>
<title>{blog_title}</title>
</head>
<body>
{blog_entries}
</body>
</html>
注意:
在使用<? echo base_url();?> 之前
先设置
config/autoload.php 中的
$autoload[‘helper‘] = array(‘url‘, ‘form‘);
本文出自 “王尼美的成人之路” 博客,请务必保留此出处http://8335914.blog.51cto.com/8325914/1435432