[英]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- 我也浏览过其他职位,其中有人建议-
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.