dqluw20882 2016-09-16 15:02
浏览 45
已采纳

CodeIgniter通过URL进行动态数据库连接

I need to connect to a DB that's name will be entered in the URL. For Example the URL will be

http://my_url.com/comp/login

So the DB I need to connect to is 'comp'.

Is there a way to do this without defining each database in the database.php config file?

something like

define('COMP', $this -> uri -> segment(1));

and then

'database' => 'tm_'.COMP,

The above gives me an error about the $this, so I am not completely sure where I can define the COMP variable where the database config file can read it.

Thanx

  • 写回答

1条回答 默认 最新

  • dsf1222 2016-09-16 15:08
    关注

    Was missing this part :

    $CI =& get_instance();
    

    and updated the

    define('COMP', $this -> uri -> segment(1));
    

    to

    define('COMP', $CI -> uri -> segment(1));
    

    is working now

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

    报告相同问题?

    悬赏问题

    • ¥20 C语言字符串不区分大小写字典排序相关问题
    • ¥15 关于#python#的问题:我希望通过逆向技术爬取1688搜索页下滑加载的数据
    • ¥15 学习C++过程中遇到的问题
    • ¥15 关于Linux的终端里,模拟实现一个带口令保护的屏保程序遇到的输入输出的问题!(语言-c语言)
    • ¥15 学习C++过程中遇到的问题
    • ¥15 请问,这个嵌入式Linux系统怎么分析,crc检验区域在哪
    • ¥15 二分类改为多分类问题
    • ¥15 Unity微信小游戏上调用ReadPixels()方法报错
    • ¥15 如何通过求后验分布求得样本中属于两种物种其中一种的概率?
    • ¥15 q从常量变成sin函数,怎么改写python代码?