简体   繁体   中英

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. But, when I run it on PHP, it does not return any rows. I know the connection and the settings are working as when I create a simple query such as "SELECT * from ..." that works. 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??

You should change your identical operator(===) to equal operator(==). Your $rows should actually contain values and you can test that by doing

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)

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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