Ankur
Ankur

Reputation: 31

PHP , MSSql server connection timeout

I have a question please help me. I want to connect mssql server using php, I want to set a time limit of 15 seconds. I want to check if the database is reachable in 15 sec. I tried using the following code but not working. I dont want to do this by doing server level changes in php.ini file. I want to do this by php only.

ini_set('mssql.connect_timeout',15);
connection = mssql_connect($_POST['DB_HOST'],$_POST['DB_USER'],$_POST['DB_PASS']);
if(!$connection) {
    throw new Exception('Unable to connect database');
} else {
    $database = mssql_select_db($_POST['DB_NAME'], $connection);
    if(!$database)  {
        throw new Exception('Unable to select database');
    } else {
        echo "Connected";
    }
}

Upvotes: 2

Views: 9657

Answers (2)

Navnish Bhardwaj
Navnish Bhardwaj

Reputation: 1718

You can initilize local settings in a php file. You can set values like max_execution_time, default_socket_timeout, memory_limit, mysql.connect_timeout, user_ini.cache_ttl, display_errors and log_errors it always nice to check the errors in PHP files with error_reporting.

Below is the sample script to set ini_set values.

If you dont want to set every time in PHP script simply set the values in php.ini file on your server/machine.

<?php  
ini_set('max_execution_time', 6000);  
ini_set("default_socket_timeout", 6000);  
ini_set('memory_limit','256M');   
ini_set('mysql.connect_timeout', 6000);  
ini_set('user_ini.cache_ttl', 6000);  
ini_set('display_errors',0);    
ini_set('log_errors',1);    
error_reporting(E_ALL);    
//phpinfo();  

Upvotes: 1

Arunendra
Arunendra

Reputation: 570

while setting the set_time_limit(), the duration of sleep() will be ignored in the execution time. The following illustrates:

<?php
echo '<html><body>';
set_time_limit(1);
$i = 0;
while($i++ < 100000001){
        if($i % 100000 == 0){
                echo $i / 100000, "<br/>\n";
        }
}
echo "done.<br/>\n";

// will not echo 'done.'.
?>

Want more info see here:- http://php.net/manual/en/function.set-time-limit.php

Upvotes: 1

Related Questions