2011-04-28 40 views
0

想知道是否有人能夠幫助我理解我的代碼爲什麼不輸出數據。我在Microsoft SQL Server 2008中創建了一個T-SQL查詢。查詢工作正常,並在SQL管理工作室中顯示所有正確的數據。下面是數據的外觀在SQL Server Management Studio中:PHP使用sqlsrv_query顯示T-SQL語句

block sequence|number of ties|percent_of_q4|number of q3 ties|percent of q3 ties| quality 
0 1108 11.34296 37 3.33935 1 
1 1094 31.11517 66 6.032907 1 
2 1109 21.633 53 4.77908 1 

當我試圖輸出使用完全相同的查詢一個簡單的PHP腳本的數據,沒有數據顯示。

發生這種情況是因爲sqlsrv_query不喜歡我的查詢嗎? sqlsrv_fetch_array是否不像我查詢中的「IS NOT NULL」?我已經完全沒有想法嘗試什麼......任何人都可以提供任何建議,爲什麼這個相當簡單的腳本似乎沒有工作?任何答覆將不勝感激!

如果有什麼不清楚的地方,請告訴我。

乾杯, 尼爾

PHP腳本如下:

 <?php 

     /*data base connection */ 



     $serverName = ".\SQLEXPRESS"; 

     $connectionOptions = array("Database"=>"V6_HOLLTS479_20101015_subset", 

     "UID"=>"username", 

     "PWD" => "password"); 



     /* Connect using Windows Authentication */  

     $conn = sqlsrv_connect($serverName, $connectionOptions); 



     /* Check whether connnection is established */ 

     if($conn === false) 

     { 

      die(print_r(sqlsrv_errors(), true)); 

     } 



     /* SQL query */ 

$tsql = " 

DECLARE @block_size AS real 



    DECLARE @threshold_1 AS real 

DECLARE @threshold_2 AS real 

    DECLARE @threshold_3 AS real 

DECLARE @threshold_4 AS real 







    --Set variables 



    SET @block_size = 200.0 



    SET @threshold_1 = 50 -- GISCat4HighThresh 

    SET @threshold_2 = 75 -- GISCat3HighThresh 

    SET @threshold_3 = 25 -- GISCat4LowThresh 

    SET @threshold_4 = 50 -- GISCat3LowThresh 



SELECT 

    sub_t.block_sequence , 

    sub_t.number_of_ties, 

    tie_q_4.number_of_ties AS number_of_q4_ties, 

    (CAST(tie_q_4.number_of_ties AS real)/CAST(sub_t.number_of_ties AS real))*100.0 AS percent_of_q4_ties, 

     tie_q_3.number_of_ties AS number_of_q3_ties, 

    (CAST(tie_q_3.number_of_ties AS real)/CAST(sub_t.number_of_ties AS real))*100.0 AS percent_of_q3_ties, 



     --The next column shows the block quality. This is currently 4,3,1 based on the threshold rules. 

    CASE WHEN (

     (CAST(tie_q_4.number_of_ties AS real)/CAST(sub_t.number_of_ties AS real))*100.0 > @threshold_1 

      OR 

     (CAST(tie_q_3.number_of_ties AS real)/CAST(sub_t.number_of_ties AS real))*100.0 > @threshold_2 

      ) THEN 4 

     WHEN (

     (CAST(tie_q_4.number_of_ties AS real)/CAST(sub_t.number_of_ties AS real))*100.0 > @threshold_3 

      OR 

     (CAST(tie_q_3.number_of_ties AS real)/CAST(sub_t.number_of_ties AS real))*100.0 > @threshold_4 

      ) THEN 3 

     ELSE 1 



        END AS quality 



     FROM (

     SELECT 

      FLOOR(CAST(image_sequence AS real)/@block_size) AS block_sequence , 

      COUNT(image_sequence) AS number_of_ties 

      FROM database 

      GROUP BY 

       FLOOR(CAST(image_sequence AS real)/@block_size) 

      ) AS SUB_T 



     LEFT JOIN (

     SELECT FLOOR(CAST(image_sequence AS real)/@block_size) AS block_sequence , 

      COUNT(image_sequence) AS number_of_ties 

      FROM database 

      WHERE 

       quality = 4 

      GROUP BY 

      FLOOR(CAST(image_sequence AS real)/@block_size) 

     ) AS tie_q_4 ON sub_t.block_sequence = tie_q_4.block_sequence 



     LEFT JOIN (

     SELECT 

      FLOOR(CAST(image_sequence AS real)/@block_size) AS block_sequence , 

      COUNT(image_sequence) AS number_of_ties 

      FROM database 

      WHERE 

       quality = 3 

      GROUP BY 

      FLOOR(CAST(image_sequence AS real)/@block_size) 

     ) AS tie_q_3 ON sub_t.block_sequence = tie_q_3.block_sequence 


     WHERE sub_t.block_sequence IS NOT NULL 
     ORDER BY block_sequence 

"; 




$result = sqlsrv_query($conn, $tsql); 



$row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC); 

print("<pre>"); 

print_r($row); 

print("</pre>"); 

回答

0

我有一個類似的問題,我試過各種然後又馬上回基礎。如果您嘗試

$query=sqlsrv_query($conn, $tsql); 
$result=sqlsrv_fetch_array($query); 
print_r($result); 

應該做的伎倆