doumeng3345 2016-06-02 22:54
浏览 85
已采纳

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

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
);
  • 写回答

1条回答 默认 最新

  • douchi1945 2016-06-02 23:03
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号