首先安利下ci官方文档
http://codeigniter.org.cn/user_guide/
常规的一些操作
读数据
$this->load->database();
$query = $this->db->query('SELECT * FROM news');
//以对象形式
foreach ($query->result() as $row)
{
echo $row->id;
echo $row->title;
echo $row->slug;
echo $row->text;
}
//以数组形式
foreach ($query->result_array() as $row)
{
echo $row['id'];
echo $row['title'];
echo $row['slug'];
echo $row['text'];
}
//直接打印 单结果标准查询(对象形式)
$query = $this->db->query('select text from news');
$row = $query->row();
echo $row->text;
//直接打印 单结果标准查询(数组形式)
$row = $query->row_array();
echo $row['text'];
//使用查询构造器查询数据 先表名、对象掏出字段
$query = $this->db->get('news');
$row = $query->row();
echo $row->text;
insert&update数据
//写数据
$data = array(
'title' => "我是ttle",
'slug' => "我是slug",
'text' => "我是text"
);
$this->db->insert('news', $data);
public function report_log($id){
// 系统时间
$update_time = date("Y-m-d h:i:s");
$this->load->database();
$data = array("lastRunTime"=>$update_time);
$this->db->update('newactivity', $data);
}
dump数据为csv
public function dump(){
$this->load->dbutil();
$this->load->database();
$query = $this->db->query("select * from newactivity;");
$data=$this->dbutil->csv_from_result($query);
//写文件
$this->load->helper('file');
if(!write_file('./test.csv',$data)){
echo "unable to write the file";
}else{
echo "file written";
}
// file_get_contents('./test.csv');
//下载文件
$this->load->helper('download');
force_download('test.csv', $data);
}