我刚开始尝试CodeIgniter 3.0.6,所以我没有经验。
问题
在我的localhost /(root)页面上出现此错误:
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
和
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
以下是我使用的所有CodeIgniter 3.0.6文件
/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
);
答案 0 :(得分:0)
在你的模特中:
$query = $this->$db->query('SELECT * FROM reviews');
请参阅this page:
$this->db->query('YOUR QUERY HERE');
请注意db
前面不应有美元符号。
“我没有经验”
然后我强烈建议您花几个小时阅读the entire CodeIgniter manual和follow the included tutorial。