简体   繁体   English

致命错误:在(CodeIgniter)中的非对象上调用成员函数query()

[英]Fatal error: Call to a member function query() on a non-object in (CodeIgniter)

I am new to codeigniter, this is my first model-controller connection,so anything you suggest is helpful for me and point will be considered for my future understanding,Kindly help to resolve here 我是Codeigniter的新手,这是我第一次建立模型控制器连接,因此您提出的任何建议对我都有帮助,以后我的理解将对您有所帮助,请在此处帮助解决

Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\xampp\ci\CI_test\application\models\testquery.php on line 7

My Controller: 我的控制器:

class Testdb extends CI_Controller
   {
    public function Index()
    {
        $this->load->model("testquery");
        $data['results'] = $this->testquery->query1();

        $this->load->view("view_db");
    }
   }

My Model is: 我的模特是:

class Testquery extends CI_Model
{
    public function query1()
    {
        $query = $this->db->query("SELECT * from color"); //this is line 7 error

        /**
        $sql = "SELECT * from color";       
        $query = $this->db->query($sql); 
        foreach ($query->result() as $row)
        {
           echo $row->color_id;
           echo $row->color_name;
           echo $row->color_desc;
        }
         */
        return $query->result();
    }
}

My view is: 我的看法是:

<<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
</head>
<body>
<h1>Welcome db</h1>
<?php 
print_r($results);
?>
</body>
</html>

My connection details: 我的连接详细信息:

config/database.php has- config / database.php具有-

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'varundb';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

My config/autoload.php has- 我的config / autoload.php具有-

$autoload['libraries'] = array('database');

Am i missing something? 我想念什么吗? I have gone through other posts as well Where people suggested to- 我也浏览过其他职位,其中有人建议-

  • load database in each function where i use db. 在我使用数据库的每个函数中加载数据库。
  • do not chain the query(which i will try once this is done). 不要链接查询(一旦完成,我会尝试的)。
  • change the group_name. 更改group_name。

And i am using XAMPP server, database is fine(i am seeing the table,data in it). 我正在使用XAMPP服务器,数据库很好(我正在查看表,数据)。 did all,still i get error!! 做了所有,仍然我得到错误!

试试这个:保持大小写为真$ this-> load-> model(“ Testquery”,“,TRUE);

You must need to load the database . 您必须需要加载数据库。

class Testdb extends CI_Controller
   {
function __construct() {
        parent::__construct();

        $this->load->database();
        $this->load->model("testquery");
    }
    public function Index()
    {
       $data['results'] = $this->testquery->query1();
       $this->load->view("view_db");
    }
   }

Add __construct() methods to both controller and model. __construct()方法添加到控制器和模型。 You don't need to omit those. 您无需忽略这些。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Codeigniter 致命错误:调用非对象上的成员函数 query() - Codeigniter Fatal error: Call to a member function query() on a non-object 致命错误:在Codeigniter中的非对象上调用成员函数result() - Fatal error: Call to a member function result() on a non-object in Codeigniter codeigniter致命错误在非对象上调用成员函数cookie() - codeigniter fatal error Call to a member function cookie() on a non-object 致命错误:在非对象,代码点火器控制器上调用成员函数 - Fatal error: Call to a member function, on a non-object, codeigniter controller 致命错误:在非对象错误上调用成员函数 query() - Fatal error: Call to a member function query() on a non-object Error 致命错误:在非对象表达式引擎上调用成员函数query() - Fatal error: Call to a member function query() on a non-object expressionengine 致命错误:在非对象上调用成员函数query() - Fatal error: Call to a member function query() on a non-object 致命错误:在非对象上调用成员函数query() - Fatal error: Call to a member function query() on a non-object 致命错误:在非对象中调用成员函数query() - Fatal error: Call to a member function query() on a non-object in CodeIgniter随机PHP致命错误:在... mysqli_driver.php中的非对象上调用成员函数。 - CodeIgniter Random PHP Fatal error: Call to a member function … on a non-object in … mysqli_driver.php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM