CI 数据库增删改查

  自增id

  $this->db->insert_id();

  受影响行数

  $this->db->affected_rows();

 

Active Record

  1.application/config/database.php

    $active_record=TRUE;

  2.application/config/autoload.php

    $autoload['libraries']=array('database');

  3.在配置文件中,配置表前缀后,会自动添加

  $res=$this->db->get('表名');//返回结果集对象  

  $res->result();

  

  $bool=$this->db->insert('表名',关联数组);

 

  $bool=$this->db->update('表名',关联数组,条件);

 

  $bool=$this->db->delete('表名',条件);

 

 代码:

数据库:

CREATE TABLE `blog_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` char(32) DEFAULT NULL,
`mail` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

   

 

 welcom.php

public function index()
{

/**
* 增加
*/

/*
$data=array(
'username'=>'sa',
'password'=>md5('123456'),
'mai'=>'sa@126.com',
);

$bool=$this->db->insert('user',$data);
var_dump($bool);
*/


/**
* 查询
*/
/*
$res = $this->db->get('user');
var_dump($res);
foreach ($res->result() as $item){
echo $item->username.'&nbsp';
echo $item->password.'&nbsp';
echo $item->mail.'&nbsp';
echo '<br/>';
}
*/
/**
* 修改
*/
/*
$bool=$this->db->update('user', $data, array('id'=>9));
var_dump($bool);
*/

/**
* 删除
*/
/*
$bool=$this->db->delete('user',array('id'=>9));
var_dump($bool);
*/

 

// $this->load->view('welcome_message');
}

 

  

访问地址:http://localhost:8899/Project/index.php

 

posted on 2015-03-21 10:52  ziyi_ang  阅读(235)  评论(0编辑  收藏  举报

导航