CodeIgniter多数据库连接报错:无效数据库连接组(siswa2)问题排查
Hey there, let's break down exactly why you're hitting this error and how to fix it quickly.
The Root Cause
The error message is pretty straightforward: your CodeIgniter app is trying to use a database connection group named siswa2, but you haven't defined this group in your config/database.php file. You only shared the $db['default'] section in your question—so that's the missing piece!
Step 1: Add the siswa2 Connection Group to database.php
Open up config/database.php and add a new configuration block for siswa2 right after your default group. Here's a template you can use (replace the placeholder values with your actual database credentials):
$db['siswa2'] = array( 'dsn' => '', 'hostname' => 'your_siswa2_database_host', // e.g., 'localhost' 'username' => 'your_siswa2_db_username', 'password' => 'your_siswa2_db_password', 'database' => 'siswa2_database_name', 'dbdriver' => 'mysqli', // Match your default driver if needed '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 );
Step 2: Make Sure You're Calling the Connection Correctly
Loading the database library via autoload only initializes the default connection group. To use siswa2, you need to explicitly load it in your controller or model:
// Load the siswa2 database connection (TRUE returns a standalone object) $siswa2_db = $this->load->database('siswa2', TRUE); // Now use this object to run queries for siswa2 $query = $siswa2_db->get('your_siswa2_table');
If you accidentally try to use $this->db for the siswa2 database, it'll still use the default group—so don't mix those up!
Step 3: Double-Check for Typos
It's easy to miss a typo: make sure the connection group name in database.php (siswa2) exactly matches what you're using in your code. CodeIgniter treats these names as case-sensitive, so Siswa2 or siswa_2 won't work if you're calling for siswa2.
内容的提问来源于stack exchange,提问作者Nadine Deskananda

