通过读取csv文件插入数据库

<?php
class Insert extends CI_Controller
{
	
	function index()
	{
		//载入数据库	
		$this->load->model('Db_content',"",TRUE);
		$file = fopen('1.csv', 'r');
		//循环
		while ($data = fgetcsv($file)) {
		//var_dump($data);
		$sqlName = $data[0];
		//转换编码结构
		$sqlName = iconv("gbk", "UTF-8", $sqlName);
		//echo $sqlName;
		$sqlevent = $data[1];
		$sqlevent = iconv("gbk", "UTF-8", $sqlevent);
		//重新组合数组
		$ndata = array('id' => '','name' => $sqlName, 'price' =>$sqlevent);		
		//var_dump($ndata);
		//ci方法插入数据库
		$this->db->insert('menu',$ndata);
//mysql_query("insert into menu(name,price) values('$sqlName','$sqlevent')",$con);

}
}
}



你可能感兴趣的:(通过读取csv文件插入数据库)