duanliang4009 2016-07-19 06:15
浏览 58
已采纳

如何解决遇到codeigniter3的PHP错误

why i can't access it?

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;

and then acces http://localhost/codeigniter/index.php/news the output is :

A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_DB_mysqli_driver::$get

Filename: models/News_model.php

Line Number: 11

Backtrace:

File: /opt/lampp/htdocs/codeigniter/application/models/News_model.php Line: 11 Function: _error_handler

File: /opt/lampp/htdocs/codeigniter/application/controllers/News.php Line: 14 Function: get_news

File: /opt/lampp/htdocs/codeigniter/index.php Line: 315 Function: require_once

Fatal error: Call to a member function result_array() on null in /opt/lampp/htdocs/codeigniter/application/models/News_model.php on line 12 A PHP Error was encountered

Severity: Error

Message: Call to a member function result_array() on null

Filename: models/News_model.php

  • 写回答

2条回答 默认 最新

  • dtr84664 2016-07-19 06:22
    关注

    change Your $this->db->get();

    It's should not be array notation

      $this->db->get['news'];
    

    it should be like

     $this->db->get('news');
    

    alternatively u can use

      $this->db->select('*');
      $this->db->from('news');
    
      $query = $this->db->get();
      return $query->result_array()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?