3

i hope someone can help me, ive been struggling on this for 3 days now.

here is my situation, i am making a website with php, and i have 2 computers as servers with wampserver...

main server is 192.168.0.10

secondary server is 192.168.0.12

and a virtual machine where im trying out if the remote conection works

my website is hosted on my main server so the conexion query is...

$conexion = mysqli_connect("localhost","root","","dbdaq");

it works fine, i even have master to master replication on the servers.

but what i need to do is that when main server is down it needs to try to conect to the database on the seondary database, so im trying to use the code...

$conexion = mysqli_connect("localhost","root","","dbdaq");


if (!$conexion){
    $conexion = mysqli_connect("192.168.0.12","root","password","dbdaq");
}

but when i manually turn of the mysql services on main server it doesnt actually try to use the other servers database...

it keeps giving me the error that says

Warning: mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it. in C:\wamp64\www\PaginaV2\Pagina\PHP y JS\Procesoalumno.php on line 2

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • Are you sure it doesn't use it? You'll get the warning regardless. – Daniel Nov 21 '17 at 00:00
  • looks like you will need to try to capture the exact error that is returned because being actively refused doesn't trigger the error you are trying to catch. – xDJR1875 Nov 21 '17 at 00:18
  • how would you guys do it ? i cant really see the error. and ive been looking for days but i cant find anything. – VesuvianMetal Nov 21 '17 at 00:38
  • your code is working, it just shows warnings. to get rid of warnings: https://stackoverflow.com/questions/17208678/remove-mysqli-connect-warning-displaying-in-page – Mark Nov 21 '17 at 01:40

2 Answers2

1

Using @ to suppress error messages is a bad idea, and a bad practice. A better solution is to set mysqli to throw exceptions on failure, and catch the exception.

You can set mysqli to throw exceptions by adding the following line before you make your connection(s).

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Using exceptions over suppressing the error, you can extract the relevant error-message and error-code if you need them for specific handling or logging, in a much cleaner way than you can with suppressing the errors.

Though it might look like its more complex, the reason for this is that we also test the second connection (which you should do anyways, regardless how you do it) - and we test the second connection the same way we test the first connection, by a second try/catch inside the first catch block.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // errors in MySQLi now throws exceptions

try {
    $conexion = mysqli_connect("localhost","root","","dbdaq");
} catch (Exception $e) {
    try {
        $conexion = mysqli_connect("192.168.0.12","root","password","dbdaq");
    } catch (Exception $e) {
        echo "Connecting to both databases failed";
        exit;
    }
}
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • Instead of localhost if i use another server IP in 1st try condition, it gives me Gateway Timeout error – Bhavik Oct 01 '21 at 12:14
0

Try this: use php's @ operator (the error control operator) to suppress the error message from the first connection failure. Try this.

$conexion = @mysqli_connect("localhost","root","","dbdaq");

if ( !$conexion ) {
    $conexion = mysqli_connect("192.168.0.12","root","password","dbdaq");
}

It seems likely your code for the failover connection works correctly, but you still get an obnoxious error message. The @ operator suppresses the message.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • i think it work in a certain way, now it is saying that acces is denied for the user, but know i dont know why because the other server has the the same users and all provileges – VesuvianMetal Nov 21 '17 at 01:34
  • thanks for evertything, the @ actually help me alot, i mannaged to fix the acces denied, i just made another user on the other database. – VesuvianMetal Nov 21 '17 at 01:41