I have some problems in my site, I got an error too many connections .
Screenshot : http://pasteboard.co/Hz7QJQR.png
the Backtrace said my error in my __construct
, model function list_slider
and my model on get function
:
this is my controller code :
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Beranda extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url', 'html'));
$this->load->model(array('slider_model', 'slider_2_model', 'group_model', 'contact_model'));
}
public function index()
{
$data['slider_data'] = $this->slider_model->list_slider();
$data['slider_2_data'] = $this->slider_2_model->list_slider();
$data['group_data'] = $this->group_model->list_group();
$data['contact_data'] = $this->contact_model->list_contact();
$data['title'] = 'Kitchenware Equipments & Utensiles - norwinskitchenware.com';
$this->load->view('fend/view_beranda', $data);
}
}
?>
and this is my model [UPDATE]:
<?php
class Slider_model extends CI_Model {
function list_slider()
{
$this->db->select('*');
$this->db->from('slider');
$query = $this->db->get();
$this->db->close();
return $query->result();
}
}
?>
I already set 'pconnect' => FALSE
and mysql.allow_persistent = OFF
What should I do ?
Thanks
In your model you should close the connection for your function.
And If you do it in your Controller
You shall close it by
__destruct()
We don't need to use
$this->db->close()
like Sulthan Allaudeen telling. Because CI & some function connect db in present have config support for auto close when manipulation complete.In codeigniter 3.1.2 i found out in
application/config/database.php
property$db['default']['pconnect']
is mean hold process after connect or we can explain like default of CI "Whether to use a persistent connection".This value should be have value is
FALSE
.Because if it value is
TRUE
. Your code access some time and have access to Database reach to limit connection of database(max_connections in/etc/my.cnf
) we will be can't connect again and will output error:mysqli::real_connect(): (08004/1040): Too many connections