php - fatal error : Call to a member function where() on a non-object

标签 php mysql codeigniter

当我在浏览器上运行此代码时,我遇到了这个 fatal error ,请解决我的问题...我不明白这段代码有什么问题...... fatal error :调用成员函数 where( )在非对象上

Fatal error: Call to a member function where() on a non-object

型号

<?php

        class News extends CI_Model
        {
        function Users(){
        //call model constructor

        parent::Model();
        //load database
        $this->load->database();
        }

        public function get_all_news(){

        $this->db->get('news');
        if($query->num_rows()>0){
        //return result set as associate array

        return $query->result_array();


        }

        }
        public function getnews($field,$param){

        $this->db->where($field,$param);

        $query=$this->db->get('news');
        // return result set as accosiate array

        return $query->result_array();
        }

        public function getnumnews(){
        return $this->db->count_all('news');
        }

        }

        ?>

Controller

<?php
    class News_data extends CI_Controller{

    public function Users(){

    // load controller parent

    parent::Controller();

    // load ‘Users’ model



    }

    public function index(){
    $this->load->model('News');
    $data['users']=$this->News->getnews('id <',5);

    $data['numusers']=$this->News->getnumnews();

    $data['title']='Displaying user data';
    $data['header']='User List';

    // load ‘Show_data’ view
    $data['title']='Display data';
    $this->load->view('Show_data',$data);

    }

    }
    ?>

查看

</head>

<body>

<h1><?php echo $header;?></h1>

<ul>

<?php foreach($users as $user):?>

<li>

<p><?php echo $user['id'].' '.$user['title'].' '.$user['tetxt'];?></p>

</li>

<?php endforeach;?>

</ul>

<p><?php echo 'Total number of users :'.$numusers;?></p>

</body>

</html>

?>

最佳答案

您不能在此处初始化数据库对象$this->db $this->db->where($field,$param); 或者您的$this->db 为空。

关于php - fatal error : Call to a member function where() on a non-object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27741471/

相关文章:

php - 如何使用foreach循环进入另一个foreach循环来遍历c​​odeigniter中的两个不同数据表?

php - 在 Codeigniter 中写入一个数组来配置?

PHP 未设置 vs array_pop?

php - Mysql - 存储和检索多行文本的最佳方式(性能和可扩展性)

mysql - Laravel:将结果与 UnionAll 和 where 条件结合起来

php - 重定向后页面未正确加载

php - 为客户提供多个数据库

PHP 显示 CMD 命令提示符窗口 (Windows)

php - 在 IIS 7.5 上安装 PHPMyAdmin

php - 无法插入到wordpress数据库中