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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀