如果database.php立即更改,如何在codeigniter中加载db

时间:2016-03-28 05:36:09

标签: database codeigniter

$result = $this->masterdb->query("select max(collegeId)+ 1 as max_id from college");

$row = $result->result_array();
$id = $row[0]['max_id'];

$database = "staging_" .date('ymdHis');
$query_database = "CREATE DATABASE IF NOT EXISTS 
                   $database
                   DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci";


/*
 * insert new college database details in config file here
 */

$result = $this->masterdb->query($query_database);
$college_id = $id;

$dats = "\$db['" .$college_id ."'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',  
    'database' => '".$database ."',
    'password' => '*******',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);" ;
$contents = $dats;
$contents .= PHP_EOL .PHP_EOL;
file_put_contents(FCPATH.'application/config/database.php', $dats, FILE_APPEND);

$college_database = $this->load->database((string)$id, TRUE);

当我想加载数据库时,我收到一个错误:

  

您已在自己的帐户中指定了无效的数据库连接组(5)   config / database.php文件。

但是当我用相同的最大ID(5)点击浏览器时。选择了数据库。如何在运行时添加配置代码后立即选择数据库。

0 个答案:

没有答案