Error : mysqli::real_connect(): (08004/1040): Too

2019-07-19 03:13发布

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

2条回答
叼着烟拽天下
2楼-- · 2019-07-19 03:38

In your model you should close the connection for your function.

And If you do it in your Controller

public function __destruct() {  
    $this->db->close();  
}  

You shall close it by __destruct()

查看更多
Anthone
3楼-- · 2019-07-19 03:39

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

查看更多
登录 后发表回答