0

How do I execute a command as and when a new mysql connection is established from php?

$con=mysqli_connect("localhost","root","","gts");

In the above exmaple, I need to pass set sql command :

sql_log_bin = 0

This will make sure that the commands executed by this connection will not be written to binary log. I have another "normal" connection that will work as usual and write all the commands to binary log.

In other words I need 2 connections to mysql one with disabled binary logging. I should be able to decide which one to use. How is it done in PHP?

2 Answers 2

2

First create two connections.

$con1=mysqli_connect("localhost","root","","gts");
$con2=mysqli_connect("localhost","root","","gts");

Then on one of them disable your binlog

mysqli_query($con1, "SET sql_log_bin = 0");
Sign up to request clarification or add additional context in comments.

Comments

1

As you need two connections with different parameters, just establish two connections:

$con1 = mysqli_connect("localhost","root","","gts");
$con2 = mysqli_connect("localhost","root","","gts");

Then have a look at mysqli_options function:

bool mysqli_options ( mysqli $link , int $option , mixed $value );

In your case, it'll be something like

mysqli_options ( $con1 , MYSQLI_INIT_COMMAND , "SET sql_log_bin = 0" );

to turn this binary log off on connection 1.

Finally, when you need to run any queries, use $con1 if you do not need binary logging and $con2 if you do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.