ci-crud增删改查基本操作
1.准备news表格,id,title,slug,text
2.新建Crud.php,并继承
class Crud extends CI_Controller
3.
public function __construct()
{
parent::__construct();
$this->load->database();
}
4.新增记录
public function c()
{
$arrNews=array();
$arrNews['title']='第一条标题';
$arrNews['slug']='A';
$arrNews['text']='this is a test';
var_dump($this ->db->insert('news',$arrNews)) ;
echo $this ->db->insert_id();
}
5.通过id读取一条
public function r()
{
$arrParams=$this->uri->uri_to_assoc();
if(empty($arrParams)||empty($arrParams['id'])){
echo 'no';
return;
}
$this->db->from("news")->where('id=',$arrParams['id']);
$res=$this->db->get();
var_dump($res->result());
}
6.读取全部
public function ra()
{
$res=$this->db->get("news");
var_dump($res->result());
}
public function u()
{
$this->db->set('title','test for title 2');
$this->db->where('id',2);
var_dump($this->db->update('news'));
}
7.删除一条 public function d()
{
$this->db->where('id',3);
var_dump($this->db->delete('news'));
}