2

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";
    }
}
1
  • see it: set_time_limit(0); ini_set('mysql.connect_timeout','0'); ini_set('max_execution_time', '0'); Commented Sep 14, 2015 at 7:48

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Dear Arunendra, you are not getting my point, actually I just want to check if my database is reachable or not in 15 secs only for a particular page only
1

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();  

1 Comment

Dear Navnish, I am doing the same in the above code but its not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.