The connection to the database is very fragile, so I try to test the connection before make a link to it. I found pg_connect
and mysql_ping
and how-do-i-ping-the-mysql-db-and-reconnect-using-pdo, but none has anything to do with MSSQL
or function that I can use.
Is there a way I can test the database before I connect to it?
You might set the timeout to a very small number and call something like
SELECT 1;
or
SELECT GETDATE();
and see, if there is some result coming back...
If the connection is broken, such tiny requests should break as well.
In case fragile does not mean broken, you might measure a standard call and compare the execution speed...
You could try to ping the server + corresponding port to see if server accepts connections.
$host = '127.0.0.1'; // ip address, could be localhost
$port = 1433; // your mssql port
$timeout = 1; //time to wait in seconds for response
if($fp = fsockopen($host,$port,$errCode,$errStr,$timeout))
{
// Server is OK - responding
} else {
// Server not respoded on given port in given time limit
}
fclose($fp);