为什么我无法访问它?
my model
News_model.php :
<?php
class News_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function get_news(){
$query = $this->db->get['news'];
return $query->result_array();
}
}
?>
my Controller
news.php :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class News extends CI_Controller {
public function __construct (){
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'arsip';
$this->load->view('news/index', $data);
}
}
?>
my view
news/index.php
<?php
foreach ($news as $news_item) { ?>
<h1><?php echo $news_item['title']; ?> </h1>
// <p><?php echo $news_item['text']; ?> </p>
<? } ?>
?>
routes :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['news'] = 'news';
$route['default_controller'] = 'halaman/view';
$route['(:any)'] = 'halaman/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
然后访问 http://localhost/codeigniter/index.php/news 输出是:
遇到PHP错误
严重性:注意
消息:未定义属性:CI_DB_mysqli_driver :: $ get
文件名:models / News_model.php
行号:11
回溯:
文件:/opt/lampp/htdocs/codeigniter/application/models/News_model.php 行:11功能:_error_handler
文件:/opt/lampp/htdocs/codeigniter/application/controllers/News.php 行:14功能:get_news
文件:/opt/lampp/htdocs/codeigniter/index.php行:315功能: require_once
致命错误:在null中调用成员函数result_array() /opt/lampp/htdocs/codeigniter/application/models/News_model.php on 第12行遇到PHP错误
严重性:错误
消息:在null
上调用成员函数result_array()文件名:models / News_model.php
答案 0 :(得分:1)
更改您的$ this-&gt; db-&gt; get();
它不应该是数组符号
$this->db->get['news'];
它应该像
$this->db->get('news');
或者你可以使用
$this->db->select('*');
$this->db->from('news');
$query = $this->db->get();
return $query->result_array()
答案 1 :(得分:0)
你必须像这样给出表名: -
public function get_news(){
$query = $this->db->get('news');
return $query->result_array();
}
请参阅此链接http://www.codeigniter.com/user_guide/database/examples.html#query-builder-query