doutao4480 2012-10-11 11:06
浏览 47
已采纳

在codeigniter中根据URL设置数据库(动态)

I am using codeigniter to develop my web based application. This will be used by various different clients and each will have there own database but same codebase.

I want to be able to set the name of database in database.php based on the clients who access it i.e based on the url. So if clientA.com access its then it should set the database name to clintA and if clientB.com access it then it should set it to clientB.com.

Any idea as how should I approch this.

  • 写回答

1条回答 默认 最新

  • duanbu4962 2012-10-11 11:15
    关注

    There are several possible ways, but probably the easiest is to use database groups in your config file. In your application/config/database.php file use a multi-dimensional array to create different groups for each of your clients. So you could have:

    $db['clientA']['hostname'] = ...
    $db['clientA']['username'] = ...
    etc...
    $db['clientB']['hostname'] = ...
    $db['clientB']['username'] = ...
    etc...
    

    Now in your application you can just pass the desired group to your load command:

    $this->load->database('clientA');
    

    Now combine that with getenv to grab the host name and you should be all set:

    $clientHost = getenv('HTTP_HOST');
    $this->load->database($clientHost);
    

    Of course there could be more logic needed, but that should get you started.

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

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大