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 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误