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
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-
$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-
$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). 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. You don't need to omit those.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.