2016-07-21 38 views
1

我有一個網站從我的MSSQL服務器提取數據。我正在使用函數爲報表構建表。下面是我得到了什麼:PDO PHP選擇不同的查詢不適用於mssql

function BeginTable($rowCount,$headings,$searchValue,$ReportName,$OneButton,$NewSearch) 
{ 
    try{ 
    $StateSelectSQL = "select distinct State from pmdb.MaterialTracking where State is not null"; 
    var_dump($StateSelectSQL);echo " What!<br>"; 

    $getSelect = $conn->query($StateSelectSQL); 
    var_dump($getSelect);echo " When!<br>"; 

    $StateSelectNames = $getSelect->fetchALL(PDO::FETCH_ASSOC); 
    var_dump($StateSelectNames);echo " Where!<br>"; 
    } 
    catch(Exception $e) 
    { 
     echo "Something went wrong"; 
     die(print_r($e->getMessage())); 
    } 

我想這太:

try{ 
     $StateSelectSQL = "select distinct State from pmdb.MaterialTracking where State is not null"; 
     var_dump($StateSelectSQL);echo " What!<br>"; 

     $getSelect = $conn->prepare($StateSelectSQL); 
     $getSelect->execute(); 

     //$getSelect = $conn->query($StateSelectSQL); 
     //var_dump($getSelect);echo " When!<br>"; 

     $StateSelectNames = $getSelect->fetchALL(PDO::FETCH_ASSOC); 
     var_dump($StateSelectNames);echo " Where!<br>"; 
    } 
    catch(Exception $e) 
    { 
     echo "Something went wrong<br>"; 
     die(print_r($e->getMessage())); 
    } 

第二和第三var_dump「從來沒有任何顯示和代碼(此處未示出)的其餘部分不得到運行。如果我註釋掉$getSelect$StateSelectNames行(其中var_dump位於它們下面),那麼其他所有內容都可以正常工作。

這裏是我的DBConn.php文件,是includedFunction以上:

$conn = new PDO("sqlsrv:server=$servername;database=$dbname", $username,$password); 
    //set the PDO error mode to exception 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $conn->setAttribute(PDO::SQLSRV_ATTR_QUERY_TIMEOUT, 10); 

什麼是錯行$getSelect = $conn->query($StateSelectSQL);我無法弄清楚。我試過在我的foreach這樣使用它:

foreach($conn->query($StateSelectSQL) as $StateName) 

但是這也行不通。它再次停在這條線上,不再進一步。我唯一能想到的就是我的SQL搞砸了,但是當我在SSMS中運行它時,它工作正常!

發生了什麼事?

回答

0

我已經拉我的頭髮一天後想通了!我不得不include我的DBConn.php裏面的function。之後它工作。我不知道爲什麼這很重要,因爲它是在文件開頭的included。如果有人能解釋爲什麼我會很感激!

現在看起來是這樣的:

function BeginTable($rowCount,$headings,$searchValue,$ReportName,$OneButton,$NewSearch) 
{ 
    try{ 
     include("DBConn.php"); 
     $SelectSQL = "select distinct State from pmdb.MaterialTracking where State is not null order by State";    

     $getSelect = $conn->prepare($SelectSQL); 

     $getSelect->execute(); 

     $StateSelectNames = $getSelect->fetchALL(PDO::FETCH_ASSOC); 

     } 
     catch(Exception $e) 
     { 
      echo "Something went wrong<br>"; 
      die(print_r($e->getMessage())); 
     } 
1

嘗試在使用fetchAll之前準備並執行SQL。還應考慮啓用異常模式,如果您還沒有將您的語句包含在try catch中 - 這應該標記任何問題(例如,您的應用程序數據庫用戶沒有權限訪問模式或語法錯誤等)

異常:

See this stack overflow post for info about how to enable

併爲您的代碼:

try { 
    $sql = " 
    SELECT DISTINCT State 
     FROM pmdb.MaterialTracking 
    WHERE State IS NOT NULL 
    "; 

    $sth = $conn->prepare($sql); 
    $sth->execute(); 

    $rowset = $sth->fetchAll(PDO::FETCH_ASSOC); 
    print_r($rowset); 

} catch PDOException($err) { 
    echo "Something went wrong". 
    echo $err; 
} 
+0

我加入了嘗試捕捉周圍(見上文),但仍然只有後到達第一個'var_dump'然後什麼!它不會拋出錯誤。 – Mike