PHP Database Connection - Calling Multiple Times - php

I'm using the following code to connect to my Database - PHP7 to MySQL
<?php
?>
The connection works fine.
My question is I'm calling this code around 5 times for each page request. This is because its included different functions.
Does this mean I'm making 5 DB connects for each page request? Or is it persistent and only called once?
If it is calling multiples times I could use a global to keep it to once.
thanks

You are using object, which creates connection in constructor. You will have as many connections as instances of database will be created.
If you create database instance, set it to some variable and use connection from this instance - there will be only one connection.
If you are creating database instance for each query - there will be many connections.

Related

Problem of distinguishing connections to multiple databases

I'm a beginner in laravel 8, and I'm trying to make the site connect to several databases, in this case there are two at the moment.
To make the multiple connections I used this site: https://themewp.inform.click/fr/comment-utiliser-la-fonctionnalite-de-connexions-a-plusieurs-bases-de-donnees-de-laravel-sur-un-site-web/
I have my first connection called mysql, and the second is called mysql2. But the pages that have to use the first connection still go to the second one. I have specified the connection they should use. So I would like to know how to make them distinguish, or if there is a method to solve this problem.

Unused write database connections

This issue was raised using Laravel 5.0.
My project's database setup consists of 1 write node and multiple read-replicas (postgresql). Everytime a connection is initiated for any query, eg:
php
<?php $user = \App\User::find(1); ?>
... a connection is made to the write node. This occurs even when no writing queries are run (including set names 'utf8', etc); a connection will be set up, but all the SELECT queries are run correctly on the read-replicas.
How can I avoid this write connection if I don't need/use it for read-only requests?
There are two classes maintaining the DB connections: Illuminate/Database/DatabaseManager and Illuminate/Database/Connectors/ConnectionFactory.
When any Laravel class want to use a DB connection, it makes a call to DatabaseManager::connection(), which eventually request for an actual connection via ConnectionFactory::make().
Your issue lies here, the underlying of make() process creates both 'read' and 'write' connections at the same time. So 'write' connection is always established. It is a behavior of ConnectionFactory.
So the best way is opening an issue on Laravel asking the dev team whether they would like to improve the connection establishment only when it is actually needed.
Edited:
I just found that you already opened an issue :-)
https://github.com/laravel/framework/issues/10337

Connect to Multiple Databases in same/one connection

How could this be done within PHP. without reloading or adding an entire new connection.
MySQL has one connection, that gives you access to multiple databases.
Where you could call,
$this->db = my_database_connection;
$this->db->database_one->query();
$this->db->database_two->query();
and when those run, it understands which database it needs to utilize without creating a new db connection.
Is this even possible?
I have an application I want to run queries and data from multiple databases (with a heavy load of traffic) but I don't want to have it use 2 connections per user, seems inefficient. The databases have the same credentials for the connection (host,pass,user) but I have many database within that connection.

Where does codeigniter create and destroy mysql connection and is the db class singleton?

If we have multiple database groups in database.php:-
1) Do the connections of all of them are made even only one has to be used in a particular call. ie. if i have database groups a,b
And in my call i load model that is loading only group b.
2) If i have loaded two models in my controller and if both of them are loading same databases, would different connection will be made or same connection will be shared.
Ex:- controller mycont.php has following:-
$this->load->model('model1');
$this->load->model('model2');
If both model1.php and model2.php has following:-
$this->load->db('connection_name');
3) Where are the connections closed.
Ex:- If i have following code:-
$this->databaseFunc();//completes the database work nothing required after this
here a curl call is made which takes long time
So when does database connection is closed, after curl or it gets closed itself on over exceeding mysql_wait_time configuration at mysql server.
Hope the answer to this question will prove useful for understanding DB with codeigniter in a better way.
In CI, each library is a singleton. It is created on load->library and destroyed at the end of the request.
The database lib handle database connection, so the connection is closed when the library is destroyed. It has nothing to do with curl.
I've never tryed it but it should work like that.

Can I call DB:connect without affecting the currently active DB?

I'm trying to merge to php code bases which each use a different DB. Can I call DB::connect which changing which DB is considered active? Or can I save the active DB and restore it after calling connect?
Edit: The problem is one code base uses mysql_query() without providing $link_identifier, and I don't want to change all the calls, but I still want to be able to open a 2nd DB connection. Right now it works depending on the order in which I connect to the DBs, which is a pain.
DB::connect creates a connection resource. You can connect to many different databases/servers at the same time. The connection resource contains the database being used. Just store the returned connection reference in different variables and you will be fine.
Connecting to more than one database at the same time is actually very common. For example, connecting to a slave DB for SELECTs and master DB for INSERTs, UPDATEs and DELETEs.
When you create a new connection to the DB, a resource link is usually returned or stored as a member of the DB wrapper class or whatever. You MUST use this link (stored in a variable, for instance) each time you call query functions, etc. If you do not specify a link, the last opened link will be used instead which could be disastrous. If you are diligent about using the correct created link, however, then there will be no problem at all.

Categories