您已在codeigniter

时间:2018-04-27 12:53:27

标签: php mysql database codeigniter continuous-integration

所以我尝试连接到代码点火器中的多个数据库,但它一直显示此错误:&#34;您在config / database.php文件中指定了一个无效的数据库连接组(siswa2)。&#34; < / p>

问题是什么?

我在自动加载上做了一点改动:

配置/ autoload.php

$autoload['libraries'] = array('database');

config / database.php - 数据库配置

<?php
defined('BASEPATH') OR exit('No direct script access allowed');


$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'siswa1',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

$db2['siswa2'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'siswa2',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

模型/ Siswa_model.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Siswa_model extends CI_Model {

 private $db2;

 public function __construct()
 {
  parent::__construct();
         $this->db2 = $this->load->database('siswa2', TRUE);
 }

 public function get_db()
 {
  return $this->db->get('siswa');
 }
 public function get_db2()
 {
  return $this->db2->get('siswa');
 }
}

控制器/ Siswa.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Siswa extends CI_Controller {
public function index(){

// load siswa_model
$this->load->model('siswa_model');

// Database 1
$data['data1'] = $this->siswa_model->get_db();

// Database 2
$data['data2'] = $this->siswa_model->get_db2();

$this->load->view('siswa', $data);
}
}

1 个答案:

答案 0 :(得分:1)

在数据库配置中,键入了错误的变量名称。

更改:

$db2['si`swa2'] 

$db['siswa2']