模型中的 CodeIgniter 未定义变量 $db

CodeIgniter undefined variable $db in model

我刚开始尝试使用 CodeIgniter 3.0.6,所以我的经验为零。

问题

在我的本地主机/(根)页面上出现此错误:

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
);

在您的模型中:

$query = $this->$db->query('SELECT * FROM reviews');

参见this page

$this->db->query('YOUR QUERY HERE');

注意 db 前面不应有美元符号。


"I have zero experience"

那我强烈建议你花几个小时阅读the entire CodeIgniter manual, and follow the included tutorial