简体   繁体   English

PHP,MSSql服务器连接超时

[英]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. 我想使用php连接mssql服务器,我想将时间限制设置为15秒。 I want to check if the database is reachable in 15 sec. 我想检查数据库在15秒内是否可以访问。 I tried using the following code but not working. 我尝试使用以下代码,但无法正常工作。 I dont want to do this by doing server level changes in php.ini file. 我不想通过在php.ini文件中进行服务器级别的更改来做到这一点。 I want to do this by php only. 我只想通过php做到这一点。

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";
    }
}

while setting the set_time_limit(), the duration of sleep() will be ignored in the execution time. 设置set_time_limit()时,sleep()的持续时间将在执行时间内被忽略。 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 想要更多信息,请参见此处: -http : //php.net/manual/zh/function.set-time-limit.php

You can initilize local settings in a php file. 您可以在php文件中初始化本地设置。 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. 您可以设置诸如max_execution_time,default_socket_timeout,memory_limit,mysql.connect_timeout,user_ini.cache_ttl,display_errors和log_errors之类的值,使用error_reporting检查PHP文件中的错误总是很不错的。

Below is the sample script to set ini_set values. 以下是用于设置ini_set值的示例脚本。

If you dont want to set every time in PHP script simply set the values in php.ini file on your server/machine. 如果您不想每次都在PHP脚本中进行设置,只需在服务器/机器上的php.ini文件中设置值即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM