关于CI连接数据,基本上是这样的几个步骤:1.配置文件 2.创建一个模型3.在控制器中让数据显示出来。我们来看配置文件:
上面是配置文件的设置,然后我们在model中连接数据库:
class News_model extends CI_Model { function _construct() { parent::_construct(); $this->load->database(''); } function getNews() { $query=$this->db->query("select title,comment from base1"); return $query->result(); } }然后我们在controller中将数据库中的文件显示出来
$arr=$this->News_model->getNews(); foreach($arr as $row) { echo $row->title."<br/>"; echo $row->comment; }