【问题标题】:how to solve A PHP Error was encountered codeigniter3如何解决 Codeigniter 3 遇到 PHP 错误
【发布时间】:2016-11-21 21:24:58
【问题描述】:

为什么我不能访问它?

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 功能: 需要一次

致命错误:调用成员函数 result_array() on null in /opt/lampp/htdocs/codeigniter/application/models/News_model.php 上 第 12 行遇到 PHP 错误

严重性:错误

消息:在 null 上调用成员函数 result_array()

文件名:models/News_model.php

【问题讨论】:

  • 应该是这样 $this->db->get('news');

标签: php codeigniter mysqli


【解决方案1】:

改变你的 $this->db->get();

它不应该是数组表示法

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

应该是这样的

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

你也可以使用

  $this->db->select('*');
  $this->db->from('news');

  $query = $this->db->get();
  return $query->result_array()

【讨论】:

    【解决方案2】:

    你必须像这样给表名:-

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多