简体   繁体   English

带有sqlsrv_query的SELECT语句未返回结果

[英]SELECT statement with sqlsrv_query not returning results

I have the following SELECT statement that is 'working' on the SQL Server query tool and returns rows. 我在SQL Server查询工具上有以下正在运行的SELECT语句,并返回行。 But, when I run it on PHP, it does not return any rows. 但是,当我在PHP上运行它时,它不返回任何行。 I know the connection and the settings are working as when I create a simple query such as "SELECT * from ..." that works. 我知道连接和设置的工作方式就像创建一个简单的查询(例如“ SELECT * from ...”)一样有效。 here is my code. 这是我的代码。

$tsql = "SELECT  [d].[Device_Type_Name],
           [a].[Company_ID] AS [Alliance],
           [a].[Device_Address]
           FROM [dbo].[AL_LKP_User_Device] AS [a]
           INNER JOIN [dbo].[AL_LKP_User] AS [b]
           ON [a].[Login_ID] = [b].[Login_ID]
           INNER JOIN [dbo].[AL_LKP_NOTIFICATION_MASTER_LOG] AS [c]
           ON [b].[Login_ID] = [c].[Login_ID]
           INNER JOIN [dbo].[AL_LKP_Device_Type] AS [d]
           ON [a].[Device_Type_ID] = [d].[Device_Type_ID]";

$stmt = sqlsrv_query( $conn, $tsql);
    if( !$stmt )
    {
        die( print_r( sqlsrv_errors(), true));
    }

    $rows = sqlsrv_has_rows($stmt);
     if ($rows === true)
          echo "There are rows. <br />";
       else
      echo "There are no rows. <br />";

I keep getting the there are no rows when there are actually rows there. 当实际上有行时,我不断得到行。 Anything that I need to change in the SQL statement?? 我需要在SQL语句中进行任何更改吗?

You should change your identical operator(===) to equal operator(==). 您应该将相同的运算符(===)更改为相等的运算符(==)。 Your $rows should actually contain values and you can test that by doing $ rows实际上应该包含值,您可以通过执行以下操作来测试

print_r($rows) 

or 要么

vardump($rows)

I tested this: 我测试了这个:

$rows = array(1=>"a",2=>"b");
if ($rows === true)
echo "There are rows. <br />";
else
echo "There are no rows. <br />";

This does return "There are no rows" but if you change "===" to "==", you should be getting the "There are rows" answer. 这的确会返回“没有行”,但是如果将“ ===”更改为“ ==”,则应该得到“有行”的答案。

1 == 1 is true 1 === '1' is not true (left val is an integer, the other is a string) 1 == '1' is true (disregards variable type) 1 == 1是true 1 === '1'不是true(左val是整数,另一个是字符串) 1 == '1'是true(忽略变量类型)

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM