Php - mysqli connect to db several times

I have an MVC framework that works like this:

/models/Mo_Sql.php (contains the connectdb function)
/models/Mo_ClassName1.php (that extends Mo_Sql)
/models/Mo_ClassName2.php (that extends Mo_Sql)
/models/Mo_ClassName...X.php (that extends Mo_Sql)
/models/... 

/rules/Ru_ClassName1.php (with private Mo_ClassName1)
/rules/Ru_ClassName2.php (with private Mo_ClassName2)
/rules/Ru_ClassName...X.php (with private Mo_ClassNameX)
/rules/Ru_......

/config/config.php with all Mo_ and Ru_ included and is called in every page of my site.

The concern that I have is that when i require_once config.php, this latter includes N Mo_ files and N Ru_ files, therefore upon construction, I believe there will be multiple db connections opened as every Mo_ file extend Mo_Sql.

My question : is it dangerous to proceed like this? Is there another way to call only once Mo_SQL and/or Mo_Sql->connectdb?

Thanks for your help

Answers


As Daniel stated, ideally you want a central point in your MVC that handles your database connections. This way it is accessibly in your entire project, plus you can easily keep track of how many connections are running at a time (preferably just one of course).

If you happen to be using a framework, look for a premade library that does these kinds of things. Most recent frameworks have something to handle this.

If you're just creating your own project in native PHP then you can just implement something that behaves like Daniel described.


Need Your Help

Upload DHT22 Sensor Data to Xively Feed

python c raspberry-pi sensor xively

I am trying to modify some of the predefined code that exists for the DHT22 sensor. I would like to modify Adafruit's DHT_Driver so that it returns an array corresponding to the Temperature value and

Why can Wordpress suddenly connect to the MySQL database server once I update the host from “localhost” to “mydomain.com” when I have VirtualHosts set up in Apache?

mysql apache wordpress virtualhost

This is a specific variation on the "can't connect" problem. In my case, I've just set up two virtual hosts in my httpd.conf listening on port 80. The declaration looks like:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.