Error : mysqli::real_connect(): (08004/1040): Too many connections [CodeIgniter v3]

匿名 (未验证) 提交于 2019-12-03 01:03:01

问题:

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

回答1:

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()



回答2:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!