2011-01-28 30 views
0

使用自定義腳本來驗證數據庫中的電子郵件地址時,我收到了一些奇怪的結果。PHP MSSQL腳本問題(警告:不是MS SQL鏈接資源)

我有一個PHP腳本,通過命令行運行來驗證電子郵件地址:

如果我運行通過腳本下面我得到的結果是:

./verify-valid-email-account.php [email protected] 
[email protected] is invalid 

的腳本設計接受多個參數(電子郵件地址),並返回每個結果:

./verify-valid-email-account.php [email protected] [email protected] 
[email protected] is invalid 
[email protected] is invalid 

然而,這個問題進來時,我嘗試做每通話3個或多個電子郵件地址,我得到後續荷蘭國際集團的錯誤:

./verify-valid-email-account.php [email protected] [email protected] [email protected] 
[email protected] is invalid 
[email protected] 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 
[email protected] 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); 
    } 

正如你可以看到我打開到數據庫的連接和前選擇dabase for循環。在for循環中,我會在每次迭代時選擇語句並處理結果。當我完成for循環時,關閉數據庫連接。

我試着將查詢,結果和numRows變量設置爲null,但這似乎並沒有解決問題。

[更新1]

每Dereleased的建議下面,我加入上述錯誤代碼。現在的輸出顯示如下:

./verify-valid-email-account.php [email protected] [email protected] [email protected] 
[email protected] is invalid 
[email protected] 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'. 

我使用的數據庫是EMTEST,看來,它的兩個迭代循環後改變環境。

回答

0

需要查看腳本的其餘部分在做什麼,但爲什麼地球上又是通過命令行調用的呢?

嘗試這樣做,對於初學者:

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

這是enitre腳本(除了數據庫的IP地址信息,這是在if語句之外)。我打算在Perl中這樣做,但我不想嘗試下載4個不同的包來使其工作。 – jinanwow 2011-01-28 17:34:21