I'm getting the error "Severity: Warning - > mysqli :: real_connect (): MySQL server has gone away" in codeigniter when the connection to the database is persistent, that is when in the base configuration of data the pconnect parameter is left in true.
'pconnect' => TRUE
This happens when there is some downtime or when you move from one page to another.
What have I tried?
I tried changing the following property mysqli.reconnect = On
in the php.ini
and it did not work, I tried adding the following lines in the controller
$this->load->database();
$this->DB1->reconnect();
and neither.
What am I doing?
I have the connection to the main database that is initially configured with codeigniter in the file:
config / database.php
In that database, in a table I save the connection data to other databases, which I activate one by one to manage it, the problem is with the second active connection.
The second connection is active as follows:
I get the connection data of the database that I am going to administer and store it in an array, then I test the connection and if the connection is successful, I save the connection in the DB1 variable
$new_config['hostname'] = $data->db_host;
$new_config['username'] = $data->db_login;
$new_config['password'] = $data->db_password;
$new_config['database'] = $data->db_database;
$new_config['dbdriver'] = $data->db_datasource;
$new_config['dbprefix'] = strtolower($data->db_prefix);
$new_config['db_debug'] = TRUE;
$new_config['cache_on'] = FALSE;
$new_config['cachedir'] = "";
$new_config['schema'] = $data->db_schema;
$new_config['port'] = $data->db_port;
$new_config['char_set'] = "utf8";
$new_config['dbcollat'] = "utf8_general_ci";
if ($data->db_persistent) { //verifico si guardé la conexión cómo persistente
$new_config['pconnect'] = TRUE;
} else {
$new_config['pconnect'] = FALSE;
}
$this->load->database($new_config);
if (!$this->check_database($new_config)) {
$this->session->set_flashdata('warning', lang('user_cntrler_activator_db_con_warning'));
redirect('user/activate');
}else{
$this->session->set_userdata('active_account', $data);
$this->session->set_userdata('active_account_config', $new_config);
$this->session->set_userdata('active_account_id', $data->id);
$this->session->set_flashdata('message', lang('user_cntrler_activator_activate_success'));
redirect('dashboard');
}
$this->DB1 = $this->load->database($_SESSION['active_account_config'], TRUE);