Click here to Skip to main content
15,888,320 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

Say I want to connect to a database in a different PC through PHP. So I could do something like this,

PHP
<?php
    $username = "root";
    $password = "sa";
    $host = "192.168.2.20";
    $database = "database";

    $link = mysql_connect($host, $username, $password);
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
                
    mysql_select_db ($database);
?>


That 192.168.2.20 IP (or the PC) exist in the network, and say it shutdown and try to connect into it as above. Before report the connection error by the above PHP code segment, it'll retry till the connection timeout.

I want to find out that the database is available before inert data into it and notify to the user if he/she cannot do that. Hope you all are clear with my requirement.

Thanks in advance :)
Posted

1 solution

You could try reducing the connection timeout: PHP mysql config page[^], or you could try connecting to the server using socket_connect[^] using a short timeout to see if it responds.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900