您在codeigniter中指定了无效的数据库连接组

bybem2ql  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(360)

因此,我尝试在CodeIgniter中连接到多个数据库,但它始终显示以下错误:“您在config/database.php文件中指定了无效的数据库连接组(siswa2)。”
问题是什么?
我在自动加载上做了一些改变:
配置/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\u 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);
}
}
lf3rwulv

lf3rwulv1#

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

$db2['si`swa2']

$db['siswa2']

相关问题