I just started experimenting with CodeIgniter 3.0.6 so I have zero experience.
Problem
Getting this error on my localhost/ (root) page:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: db
Filename: models/model_reviews.php
Line Number: 8
Backtrace:
File: C:\xampp\htdocs\application\models\model_reviews.php
Line: 8
Function: _error_handler
File: C:\xampp\htdocs\application\controllers\Welcome.php
Line: 26
Function: getReviews
File: C:\xampp\htdocs\index.php
Line: 315
Function: require_once
and
An uncaught Exception was encountered
Type: Error
Message: Cannot access empty property
Filename: C:\xampp\htdocs\system\core\Model.php
Line Number: 77
Backtrace:
File: C:\xampp\htdocs\application\models\model_reviews.php
Line: 8
Function: __get
File: C:\xampp\htdocs\application\controllers\Welcome.php
Line: 26
Function: getReviews
File: C:\xampp\htdocs\index.php
Line: 315
Function: require_once
Here are all my CodeIgniter 3.0.6 files used
/applications/config/autoload.php
$autoload['libraries'] = array('database');
/application/views/welcome_message.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body>
<div id="container">
<h1><?php echo $page_header; ?></h1>
<div id="body">
<code>
<?php
foreach($reviews as $review){
echo $review->name.'<br>';
}
?>
</code>
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/Welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds.
</div>
</body>
</html>
/application/controllers/Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('model_reviews');
$data['title'] = 'Reviews';
$data['page_header'] = 'Reviews hier:';
$data['reviews'] = $this->model_reviews->getReviews();
$this->load->view('welcome_message', $data);
}
}
?>
/application/models/model_review.php
<?php
class Model_reviews extends CI_Model{
function __constuct(){
parent::__constuct(); // Call the Model constructor
}
function getReviews(){
$query = $this->$db->query('SELECT * FROM reviews');
if($query->num_rows() > 0){
return $query->result();
}else{
return null;
}
}
}
?>
/application/config/database.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'ci_reviews',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
In your Model:
$query = $this->$db->query('SELECT * FROM reviews');
See this page:
$this->db->query('YOUR QUERY HERE');
Notice there should be no dollar sign in front of db
.
"I have zero experience"
Then I strongly recommend you take a few hours to read the entire CodeIgniter manual, and follow the included tutorial.