Codeigniter - 我想使用/连接到不同的数据库,我的控制器和一个模型 [英] Codeigniter - I am looking to use/connect to a different database for one of my controllers and one model

查看:95
本文介绍了Codeigniter - 我想使用/连接到不同的数据库,我的控制器和一个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用/连接到我的控制器和一个模型的不同的数据库。

I am looking to use/connect to a different database for one of my controllers and one model. I posted this hear since on the forums of CI I am getting no response.

我在database.php中添加了这个:

I added this in database.php:

$db['tdb']['hostname'] = "localhost";//localhost

$db['tdb']['username'] = "username";//root

$db['tdb']['password'] = "password";//empty

$db['tdb']['database'] = "databasename";

$db['tdb']['dbdriver'] = "mysql";

$db['tdb']['dbprefix'] = "";

$db['tdb']['pconnect'] = FALSE;

$db['tdb']['db_debug'] = FALSE;

$db['tdb']['cache_on'] = FALSE;

$db['tdb']['cachedir'] = "";

$db['tdb']['char_set'] = "utf8";

$db['tdb']['dbcollat'] = "utf8_general_ci";

这是我的模型:

<?php

class Tadmin_model extends Model{

    function Tadmin_model(){

        parent::Model();

        $tdb = $this->load->database('tdb', TRUE);            
    }

    function FInsert($usernames){

        $query = $tdb->query("SELECT * FROM following");

        return $query->row();
    }    
}

?>

这是我的控制器的开始:

And this is the start of my controller:

<?php

class Tadmin extends Controller{

    function Tradmin(){

        parent::Controller();

        $this->load->model('tadmin_model');

我得到这个错误:


遇到PHP错误

A PHP Error was encountered

严重性:注意

消息:未定义的变量: tdb

Message: Undefined variable: tdb

文件名:models / tadmin_model.php

Filename: models/tadmin_model.php

行号:...

致命错误:调用成员函数

上的非对象上查询()$ b /blablabla/tadmin_model.php行
...

Fatal error: Call to a member function query() on a non-object in /blablabla/tadmin_model.php on line ...

我在这里做错了什么?

推荐答案

您的问题不在于使用CodeIgniter的数据库函数,而是在类中使用变量范围。加载模型时,您将连接到数据库并将结果分配给模型构造函数中的局部变量。当任何函数完成时,局部变量被丢弃。然后你试图调用已经抛出的$ tdb变量的query()方法,并得到一个错误。

Your issue here isn't with the use of CodeIgniter's database functions, but with variable scoping in classes. When you load your model you're connecting to the database and assigning the result to a local variable in the model's constructor. When any function finishes, the local variables are discarded. Later you try to call the query() method on the $tdb variable which has already been thrown away and get an error.

你需要存储$ this的结果 - > load-> database()在一个可用于构造函数和方法的位置。您可以将$ this-> load-> database()调用移动到controller方法中,并在每次调用模型上的Tradmin方法时连接到其他数据库。

You need to store the results of $this->load->database() in a location which is available to both the constructor and the method. You could move the $this->load->database() call into the controller method and connect to the other database every time you call the Tradmin method on your model.

另一种方法,如果你想使$ tdb可用于模型中的所有方法,就是在类上使用一个成员变量,如下所示: 。

The other way if you wanted to make $tdb available to all methods in the model is to use a member variable on the class, which would look like this....

<?php

class Tadmin_model extends Model{
    var $tdb;

    function Tadmin_model(){

        parent::Model();

        $this->tdb = $this->load->database('tdb', TRUE);            
    }

    function FInsert($usernames){

        $query = $this->tdb->query("SELECT * FROM following");

        return $query->row();
    }    
}

?>

希望有帮助。

这篇关于Codeigniter - 我想使用/连接到不同的数据库,我的控制器和一个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆