2013-11-04 43 views
2

失敗我執行這個查詢使用PHP和ODBC驅動程序MS SQL查詢在PHP中,但不是在MS SQL Server Management Studio中

$sql="DECLARE @Auftrag int; 
DECLARE @date_now datetime = getdate(); 
EXEC @Auftrag=EHS.dbo.SP_ANZEIGE 
@Tablet=1, 
@Status=0, 
@KuNr='K015538'; 
SELECT 'generatedID'[email protected];"; 

$res = odbc_exec($db1_link, $sql) or die(odbc_errormsg()); // returns resource(13) 
$firstRow = odbc_fetch_array($res); // dies error 

如果我做odbc_fetch_array錯誤「目前沒有這個結果指標的元組」被拋出。

如果我在Management Studio中運行完全相同的查詢,一切正常。它向我顯示了計算生成的ID。有什麼不同?

招呼亞歷克斯

+0

向我們展示,你是怎麼做到運行PHP此查詢 – Yang

+2

嘗試用前綴查詢:'設置nocount上'。這可以防止SQL Server發送rowcount更新,哪些UNIX客戶端有時會誤認爲實際的行集。 – Andomar

+0

WOW ...設置nocount真的做到了。我不知道爲什麼,但我真的很高興:)謝謝 – alex

回答

2

嘗試前綴與查詢:

set nocount on 

這可以防止SQL Server發送的行數更新,它的UNIX客戶有時會誤認爲是實際的行集

0

我有同樣的問題。在我來說,我正在執行這樣的

$sql = "SELECT * FROM table1"; 
$resultSet = odbc_exec($sqllink, $sql); 

while ($data = odbc_fetch_array($resultSet)) { 
    $sql = "SELECT * FROM table2"; 
    $resultSet2 = odbc_exec($sqllink, $sql);//failed here 
    while ($data2 = odbc_fetch_array($resultSet2)) { 

      //something here 

    } 
} 

,我改變了這樣的,它的工作

$sql = "SELECT * FROM table1"; 
$resultSet = odbc_exec($sqllink, $sql); 

// Create an array and store the results 

$queryResult = array(); 

while ($data = odbc_fetch_array($resultSet)) { 

    // push the required content into the array 

    $queryResult[$data['id']]['name'] = $data[name]; 
} 

foreach($queryResult as $row) { 
    $sql = "SELECT * FROM table2"; 
    $resultSet2 = odbc_exec($sqllink, $sql); 
    while ($data2 = odbc_fetch_array($resultSet2)) { 

     // something here 

    } 
} 
相關問題