PHP to Duplicate Connection Variable?

So I have one PHP file I include for the slave mysql server and another for the master with the mysqli_connect info. The slave does SELECT queries, while the master does all but SELECT.

I am getting rid of the master to use the slave exclusively. But in a small pickle, what is the best way to update the connection variable in possibly hundreds of different files of code?

most pages just run SELECT queries to $slave, while others use both $slave and $master for when an update or insert is needed. Since a single server will be doing all of these now, is it possible, in my connection script that gets included, to explain with PHP that $slave and $master are now the same server connection, without having to run mysqli_connect twice?

Sort of like make $master = $slave, so both variables can effectively work for the same mysql_connect(ion).



I’m not entirely clear about your current setup, but if you are using includes for the two connections, can’t you edit those connection includes to be the same? Or make the master one just include the slave one, so they will effectively be the same include, without having to edit the include line in every script that uses them.


I could do that, but that would mean two mysqli connects to the same server when I only need one.

In the two include connection scripts, I guess I can see if connection is already open or already exists and then don’t connect again. But I’d still hate to have to connect twice just so I can have two of the same connection variables, $master and $slave.

Would be cool if, before creating $master, I see if $slave is already connected/exist and just copy the link identifier of $slave over to $master without the need of doing another mysqli_connect.

I’d really like to do mysqli_connect only once instead of twice.

Not sure if that makes sense.

I still don’t think I get the set-up, but I’ll throw some ideas in.

if(isset($slave)){ $master = $slave ;}
else{ connect }


If both connection includes just said:-

include_once "path/sql-connection.php" ;

Sweet, so it is easy as doing $master = $slave; ?

I thought mysql might complicate it. I’ll test. Cheers!


Also, to be clear, my longterm goal is to remove the double include from all the pages as I go through them, but I need to get a solution asap to ensure nothing minor or background process (or mysql from cron) that I might have forgotten about, breaks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.