PHP 使用 sqlsrv_query 显示 T-SQL 语句

发布于 2024-11-04 01:39:05 字数 4062 浏览 2 评论 0原文

只是想知道是否有人可以帮助我理解为什么我的代码不输出数据。我在 Microsoft SQL Server 2008 中创建了一个 T-SQL 查询。该查询运行良好,并在 SQL Management Studio 中显示所有正确的数据。下面是数据在 SQL Server Management studio 中的外观:

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

Just wondering if there is anyone that can help me understand why my code is not outputting data. I have created a T-SQL query in Microsoft SQL Server 2008. The query works fine and displays all the correct data in SQL management studio. Below is how the data looks in SQL Server Management studio:

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

When I try to output the data in a simple PHP script using exactly the same query, no data is shown.

Does this happen because the sqlsrv_query does not like my query? Does sqlsrv_fetch_array not like the "IS NOT NULL"in my query? I've completely run out of ideas on what to try... Can anyone provide any advice for why this fairly simple script doesn't seem to be working? Any reply would be greatly appreciated!

If anything is unclear, please let me know.

Cheers,
Neil

The PHP script is below:

       <?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>");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极致的悲 2024-11-11 01:39:05

我遇到了类似的问题,我尝试了各种方法,然后又回到了基础。如果你尝试

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

应该能成功

I had a similar issue, I tried all sorts and then went right back to basics. If you try

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

Should do the trick

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文