开发者

PHP MSSQL Scripting Issue (Warning: Not a MS SQL Link Resource)

I am getting some strange results when working with a custom script to verify email addresses in a database.

I have a php script that is ran via command line to verify email addresses:

If I run the following via script I get the result:

./verify-valid-email-account.php test@test.com
test@test.com is invalid

The script is designed to accept multiple arguments (email addresses) and returns the result for each:

./verify-valid-email-account.php test@test.com test2@test.com
test@test.com is invalid
test2@test.com is invalid

However, the issue comes in when I try to do 3 or more email addresses per call I get the following error:

./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_clos开发者_开发问答e(): 4 is not a valid MS SQL-Link resource in /scripts/verify-valid-email-account.php on line 62

If I try calling 5 email addresses to the script the first two will work fine, the last 3 will return the same error below.

Here is part of my script:

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

As you can see I open the connection to the database and select a dabase before the for loop. In the for loop I do select statement each iteration and process the result. When I get finished with the for loop I close the database connection.

I tried setting the query, result and numRows variables to null as well but that does not appear to fix the issue.

[Update 1]

Per Dereleased's recommendation below, I added the above error code. The output now shows this:

./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 48
Error: Changed database context to 'EmTest'.

The database I am using is EmTest, it appears that it changes context after two iterations of the for loop.


Need to see what the rest of the script is doing, but why on earth is this being called via command-line again?

Try doing this, for starters:

if ($argc >= 2 && false)

Please try this and post output:

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);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜