PHP MSSQL脚本问题(警告:不是MS SQL链接资源)

PHP MSSQL脚本问题(警告:不是MS SQL链接资源),php,sql-server,command-line,Php,Sql Server,Command Line,在使用自定义脚本验证数据库中的电子邮件地址时,我得到了一些奇怪的结果 我有一个通过命令行运行的php脚本,用于验证电子邮件地址: 如果我通过脚本运行以下命令,我将得到结果: ./verify-valid-email-account.php test@test.com test@test.com is invalid 脚本设计为接受多个参数(电子邮件地址),并返回每个参数的结果: ./verify-valid-email-account.php test@test.com test2@test.

在使用自定义脚本验证数据库中的电子邮件地址时,我得到了一些奇怪的结果

我有一个通过命令行运行的php脚本,用于验证电子邮件地址:

如果我通过脚本运行以下命令,我将得到结果:

./verify-valid-email-account.php test@test.com
test@test.com is invalid
脚本设计为接受多个参数(电子邮件地址),并返回每个参数的结果:

./verify-valid-email-account.php test@test.com test2@test.com
test@test.com is invalid
test2@test.com is invalid
但是,当我尝试在每次通话中输入3个或更多电子邮件地址时,会出现以下错误:

./verify-valid-email-account.php test@test.com test2@test.com test3@test.com
test@test.com is invalid
test2@test.com is invalid
Warning: mssql_query(): 4 is not a valid MS SQL-Link resource in /scripts/verify-valid-email-account.php on line 46

Warning: mssql_num_rows(): supplied argument is not a valid MS SQL-result resource in /scripts/verify-valid-email-account.php on line 48
test@test.com is invalid

Warning: mssql_close(): 4 is not a valid MS SQL-Link resource in /scripts/verify-valid-email-account.php on line 62
如果我尝试调用脚本的5个电子邮件地址,前两个将正常工作,后3个将返回下面相同的错误

以下是我脚本的一部分:

if ($argc >= 2)
 {
  //connection to the database
  $dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer");

  //select a database to work with
  $selected = mssql_select_db($myDB, $dbhandle) or die("Couldn't open database $myDB");  

   for($i=1; $i<$argc; $i++) {
     $query = "<removed as it is huge>";
     $result = mssql_query($query);

 if (!$result) {
 echo 'Error: ', mssql_get_last_message(), "\n";
 continue;                      
 } 

     $numRows = mssql_num_rows($result);
     if ($numRows == 0)
     {
      echo $argEmail . " is invalid\n";
     } else {
      echo $argEmail . " is valid\n";
     }
   }

  //close the connection to the DB.
  mssql_close($dbhandle);
  }

我使用的数据库是EmTest,它似乎在for循环的两次迭代后更改了上下文。

需要查看脚本的其余部分在做什么,但究竟为什么要通过命令行再次调用它

首先,尝试这样做:

if ($argc >= 2 && false)

请尝试此操作并发布输出:

if ($argc >= 2)
    {
        //connection to the database
        $dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer");

        //select a database to work with
        $selected = mssql_select_db($myDB, $dbhandle) or die("Couldn't open database $myDB");       

            for($i=1; $i<$argc; $i++) {
                    $query = "<removed as it is huge>";
                    $result = mssql_query($query);
                    // add these lines
                    if (!$result) { 
                        echo 'Error: ', mssql_get_last_message(), "\n";
                        continue; 
                    }
                    $numRows = mssql_num_rows($result);
                    if ($numRows == 0)
                    {
                        echo $argEmail . " is invalid\n";
                    } else {
                        echo $argEmail . " is valid\n";
                    }
            }

        //close the connection to the DB.
        mssql_close($dbhandle);
        }
如果($argc>=2)
{
//与数据库的连接
$dbhandle=mssql_connect($myServer、$myUser、$myPass)或die(“无法连接到$myServer上的SQL Server”);
//选择要使用的数据库
$selected=mssql_select_db($myDB,$dbhandle)或die(“无法打开数据库$myDB”);

for($i=1;$iThat是enitre脚本(数据库ip地址信息除外,它在if语句之外)。我本来打算用perl来做这件事,但我不想尝试下载4个不同的包来让它正常工作。很抱歉延迟了。请添加新的if块并发布新的输出,这样我们就有希望看到服务器抛出了什么错误。不过,看起来您的连接在这一点上完全消失了,所以这可能是错误没有帮助。参见上面的更新。看起来它试图在连接的中间改变DB上下文。一个小的Google表示更改的上下文可能指示查询超时。在第一个结果到第二个之间需要大约4毫秒,而它给出错误则需要大约4毫秒。
if ($argc >= 2)
    {
        //connection to the database
        $dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer");

        //select a database to work with
        $selected = mssql_select_db($myDB, $dbhandle) or die("Couldn't open database $myDB");       

            for($i=1; $i<$argc; $i++) {
                    $query = "<removed as it is huge>";
                    $result = mssql_query($query);
                    // add these lines
                    if (!$result) { 
                        echo 'Error: ', mssql_get_last_message(), "\n";
                        continue; 
                    }
                    $numRows = mssql_num_rows($result);
                    if ($numRows == 0)
                    {
                        echo $argEmail . " is invalid\n";
                    } else {
                        echo $argEmail . " is valid\n";
                    }
            }

        //close the connection to the DB.
        mssql_close($dbhandle);
        }