用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1582|回复: 9
收起左侧

[已解决] 制定数据库连接时($this -> load -> database('addtional');)报错

[复制链接]
发表于 2017-1-19 10:54:29 | 显示全部楼层 |阅读模式
本帖最后由 zhaoweizw2000 于 2017-1-20 08:32 编辑

使用CodeIgniter版本3.1.2,php-5.6.19,apache2.4
model文件
<?php
class TestDB extends CI_Model {

        public function __construct() {
                $db2=$this -> load -> database('addtional',TRUE);
                // $this -> load -> database('addtional');
        }
        public function getNews(){
                // $this->db->get('news');
                $db2->get('news');
        }
}

control文件
<?php
class TestDBController extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this -> load -> model('TestDB');
    }

    public function index() {
        $data['news'] = $this->TestDB -> getNews();
    }
}
databases.php
$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'    => 'mysql:host=localhost;port=3306;dbname=moyxk',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => 'yaomei566',
    'database' => '',
    'dbdriver' => 'pdo',
    '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
);

$db['addtional'] = array(
        'dsn'    => 'mysql:host=localhost;port=3306;dbname=moyxk',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => 'yaomei566',
        'database' => '',
        'dbdriver' => 'pdo',
        '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
);
修改成这样后,提示
Message: Undefined variable: db2
Filename: models/TestDB.php

如果使用不带参数的 $this -> load -> database('');就不报错
使用$db2=$this -> load -> database('addtional',TRUE);也是无法获取数据库连接
请问问题出在哪里

发表于 2017-1-19 11:11:56 | 显示全部楼层
請多看看 CI 手冊

1. getNews() 沒有 return;
2. 載入 Model 時無須給予承接變數
3. 欲呼叫 getNews() 時寫法錯誤
 楼主| 发表于 2017-1-19 11:30:38 | 显示全部楼层
Closer 发表于 2017-1-19 11:11
請多看看 CI 手冊

1. getNews() 沒有 return;

getNews()就是用来测试数据库是否连接正常,不用返回数据。
<?php
class TestDB extends CI_Model {

        public function __construct() {
                $db2=$this -> load -> database('addtional',TRUE);
                // $this -> load -> database('addtional');
        }
        public function getNews(){
                // $this->db->get('news');
                $db2->get('news');
        }
}
修改成这样后,提示
Message: Undefined variable: db2
Filename: models/TestDB.php
发表于 2017-1-19 11:39:40 | 显示全部楼层
抱歉沒看清你的問題

原因是 $db2 非全域變數
你在 getNews 使用 $db2 開始
也就不會有值而報錯了
 楼主| 发表于 2017-1-19 14:37:09 | 显示全部楼层
Closer 发表于 2017-1-19 11:39
抱歉沒看清你的問題

原因是 $db2 非全域變數

怎么把$db2配置成全局变量 能否写个例子?
发表于 2017-1-19 18:13:13 | 显示全部楼层
我看报错里有 $db1 未定义,可以你贴的代码里哪有 db1 呀。。。
 楼主| 发表于 2017-1-20 08:35:53 | 显示全部楼层
Hex 发表于 2017-1-19 18:13
我看报错里有 $db1 未定义,可以你贴的代码里哪有 db1 呀。。。

现在的问题是
class TestDB extends CI_Model {

        public function __construct() {
                $db2=$this -> load -> database('addtional',TRUE);
        }
        public function getNews(){
                $db2->get('news');
        }
}
  $db2=$this -> load -> database('addtional',TRUE);定义后,在getNews()的方法中提示没有定义,我想问问怎么在getNews()方法中,使用$db2这个对象
 楼主| 发表于 2017-1-20 09:06:08 | 显示全部楼层
<?php
class TestDB extends CI_Model {
        public function __construct() {
               
                // $this -> load -> database('addtional');
        }
        public function getNews(){
                // $this->db->get('news');
                $db2=$this -> load -> database('addtional',TRUE);
                $db2->get('news');
        }
}
这样就可以在这个getNews()方法中使用了。但是如果还有别的方法想使用$db2对象,还需要重新获取数据库对象
发表于 2017-1-20 11:28:05 | 显示全部楼层
zhaoweizw2000 发表于 2017-1-20 08:35
现在的问题是
class TestDB extends CI_Model {

这样改:
PHP复制代码
class TestDB extends CI_Model {
 
        public function __construct() {
                $this->db2 = $this -> load -> database('addtional',TRUE);
        }
        public function getNews(){
                $this->db2->get('news');
        }
}
复制代码


这是 PHP 知识,和 CI 无关。
发表于 2017-1-20 11:32:50 | 显示全部楼层

每次都重新获取数据库对象没关系的,不会影响性能的。

本版积分规则