0

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?

Axel
  • 3,331
  • 11
  • 35
  • 58
roger
  • 1,225
  • 2
  • 17
  • 33
  • ***"The connection to the database is very fragile"*** What makes you say this? – Pedro Lobito Apr 20 '17 at 22:16
  • why not just check the initial connection worked? –  Apr 20 '17 at 22:16
  • do you mean to just ping the machine? That might tell you that the machine is up, but doesn't necessarily mean SQL Server is running. To test if SQL is running, attempting to connect to it and waiting for a connection or a timeout is actually a reasonable approach. Or you could use telnet, as suggested here: https://dba.stackexchange.com/questions/23704/easy-way-to-check-connectivity-to-sql-server-from-client . Here are some ideas to call telnet from PHP: https://www.google.co.uk/search?client=ubuntu&channel=fs&q=call+telnet+from+PHP&ie=utf-8&oe=utf-8&gfe_rd=cr&ei=njP5WPGJAZPW8AfIpK3QCg – ADyson Apr 20 '17 at 22:19
  • @PedroLobito in this case, `fragile` means it keeps on break off and then on and then off etc. – roger Apr 20 '17 at 22:45
  • so, you don't need to ping the database but rather fix `my.cnf` – Pedro Lobito Apr 20 '17 at 22:49
  • @PedroLobito Isn't my.cnf for MySQL? OP is specifically talking about SQL Server. – ADyson Apr 21 '17 at 10:59
  • @Kim.LBy is it a network problem? Maybe first see if you can fix your network connection. – ADyson Apr 21 '17 at 10:59
  • @adyson My bad, but the problem should be related to the server config. – Pedro Lobito Apr 21 '17 at 11:58
  • @PedroLobito maybe. If the _SQL Server service_ is going up and down erratically, then yes, there could be a config issue. But it smells much more like a flakey network to me. The best test of this would be to run the OP's code on the same machine as SQL Server is installed and see if the issue persists or not. – ADyson Apr 21 '17 at 12:46
  • @Kim.LBy what error do you get when your connection fails? We might be able to use that to determine whether this is a networking issue, or a problem with SQL Server itself. – ADyson Apr 21 '17 at 12:46

2 Answers2

0

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...

Shnugo
  • 66,100
  • 9
  • 53
  • 114
0

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);
Jan Vorisek
  • 530
  • 1
  • 7
  • 17
  • I created a function to test the server but the database is stored somewhere else, I can't use the `fsockopen` to ping ip address that the database is on. – roger Apr 20 '17 at 22:49