ci mysql 加载_CI载入自定义配置文件 | 网游世界

CI版本:2.1.4

在程序设计中,需要将某些设置存入配置文件中,

如你的配置文件存储在:application/config/mysqlconfig.php

内容:<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['dbhost'] = '127.0.0.1'; // MYSQL地址

$config['dbuser'] = 'root'; // 连接用户名

$config['dbpass'] = '123456'; // 连接密码

$config['connect'] = 0; // 保持连接为1,默认为0

$config['dbname'] = 'ramble'; // 使用数据库名

?>

手动加载配置

使用:$this->load->config(‘mysqlconfig’);

使用:$this->config->item(‘dbname’, ‘mysqlconfig’);来获取指定值。

如果你想使用索引做为数组,你可以使用 $this->load->config(‘mysqlconfig’, TRUE);

使用:$this->config->item(‘dbname’,’mysqlconfig’)获取配置文件中的指定值。

使用:$this->config->item(‘mysqlconfig’);获取全部配置值。

使用:$this->config->set_item(‘dbname’, ‘item_value’);设置新值

但如果,配置名称非$config,如:<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$dbs['dbhost'] = '127.0.0.1'; // MYSQL地址

$dbs['dbuser'] = 'root'; // 连接用户名

$dbs['dbpass'] = '123456'; // 连接密码

$dbs['connect'] = 0; // 保持连接为1,默认为0

$dbs['dbname'] = 'ramble'; // 使用数据库名

?>

似乎这样不行,CI会报这样的错误:Your application/config/mysqlconfig.php file does not appear to contain a valid configuration array.

自动加载配置

在application/config/autoload.php中找到:$autoload[‘config’]项,加入”mysqlconfig”即可。

即,$autoload[‘config’] = array(‘mysqlconfig’);

忽略配置加载错误

你可以使用:$this->load->config(‘mysqlconfig’, FALSE, TRUE);

当配置文件不存在,或者配置项非$config时可以忽略这个错误。The configuration file mysqlconfig.php does not exist.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值