简体   繁体   English

PHP的sqlsrv_connect设置超时?

[英]php sqlsrv_connect set timeout?

In my case, sometimes the database that php is trying to connect to might not exist, if it doesnt exist i want to echo "Database doesnt exist"; 以我为例,有时php试图连接的数据库可能不存在,如果不存在,我想echo "Database doesnt exist";

Is it possible to set a timeout in like 5 seconds, if the database doesnt exist becouse my script takes over 50 seconds to run? 如果数据库不存在,因为我的脚本需要50秒钟才能运行,是否可以在5秒内设置超时?
It works but it takes too long to execute! 它可以工作,但是执行时间太长!

$userID=$_SESSION['userID']; 
$serverName = ''; 
$uid = '';   
$pwd = '';
$database=$_SESSION['userID']; //sometimes correct database, other times 
                                 incorrect database  
$connectionInfo = array( "UID"=>$uid,                            
                         "PWD"=>$pwd,                            
                         "Database"=>$database); 

$conn = sqlsrv_connect( $serverName, $connectionInfo);
if(!$conn ) {
echo "invalid database name";
  }

It won't tell you that the database doesn't exist, but there is a connection option for login timeout, which specifies the number of seconds to wait before failing the connection attempt. 它不会告诉您数据库不存在,但是有一个登录超时的连接选项,它指定连接尝试失败之前要等待的秒数。

You can use LoginTimeout in your connection options array like so: 您可以在连接选项数组中使用LoginTimeout ,如下所示:

$connectionInfo = array( "UID"=>$uid,                            
                         "PWD"=>$pwd,                            
                         "Database"=>$database,
                         "LoginTimeout" => 5);

Where 5 is the number of seconds you want the driver to wait before failing the attempt. 其中5是希望驱动程序在尝试失败之前等待的秒数。

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

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