search
Homephp教程php手册php中CI操作多个数据库的代码

最近有个需求,做一个独立后台出来,这个独立后台有单独的库,单独的url,单独…。但是,这里边只是一些基础数据,而使用者需要通过该库查看另一个库里的统计数据。这就带来一个问题:我需要在控制器里边可以同时操作两个数据库,而且这两个库毫不相干

其实,这不是什么难事,因为刚入手CI,所以还是费了一番周折。好在有手册。

找到数据库配置文件,添加一个新的库的连接信息。$config[XX]。
在控制器里边,
    a) $this->xx = $this->load->database(‘XX', TRUE) tip:XX表示你配置的数据库信息数组的键名,后边建议设置为TRUE,分别表示返回连接ID和启用ActionRecord类
    b) 然后在该控制器里边你就可以用 $this->xx->query()来查询你的另一个库的数据。而你另一个库是不需要建立modle文件的
代码如下:
function __construct()
{
parent::__construct();
$this->xx = $this->load->database('XX', TRUE);
$this->load->model('默认库表名');
}
function index()
{
//现在可以用$this->xx->query()来执行XX库的sql了
$this->xx->query($sql);
}
?>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.