简体   繁体   中英

sqlsrv_query executing not returning result, but result exists when code executed in sql manager

I'm trying to execute the code below, but for some reason, it is not returning any result, it is not reporting any errors, and I can't figure out why. When I paste the query directly to SQL Manager, it is executed, and I get the desired result.

I assume it's something with sqlsrv_query that is interpreting differently, but I can't figure out why.

It's a first time; I'm trying to do it with PHP so I'm struggling and I would appreciate if anyone can point me in the right direction.

 $sql = "DECLARE @YearsToPass INT
    SET @YearsToPass = 10;
    WITH cte AS
    (
    SELECT DATEPART(YY, GETDATE())- @YearsToPass + 1 as Years
    UNION ALL
    SELECT Years + 1 as Years
    FROM cte
    WHERE Years + 1 <= YEAR(GETDATE())
    )

    SELECT cte.Years, DATEFROMPARTS(cte.Years, 12, 31),  SUM(Inv.[Nabavna vrijednost])
    FROM cte
    left join  [Drezga01].[dbo].[BI_Inventory01] Inv on Inv.[Datum knjiženja] <= DATEFROMPARTS(cte.Years, 12, 31)
    group by cte.Years, DATEFROMPARTS(cte.Years, 12, 31)
    ORDER BY cte.Years DESC";
 $query = sqlsrv_query($conn_bi,$sql);
 //error handling
 if( $query === false ) {
    if( ($errors = sqlsrv_errors() ) != null) {
     foreach( $errors as $error ) {
     echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
     echo "code: ".$error[ 'code']."<br />";
     echo "message: ".$error[ 'message']."<br />";
     }
   }
  }
while($row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC)){
$zaliha_array[] = array('godina' => $row['godina'],
                        'zaliha' => abs($row['nabava']));
}

 debugVar($zaliha_array);

Array is empty, but no errors reported. Result exists and is returned when query executed in SQL Management Studio. Any ideas?

You generate statement, that has no column aliases for DATEFROMPARTS(cte.Years, 12, 31) and SUM(Inv.[Nabavna vrijednost]) and using sqlsrv_fetch_array() with SQLSRV_FETCH_ASSOC is one possible reason for your unexpected results. Try to fetch data with SQLSRV_FETCH_NUMERIC option to retrieve each row of a result set as a numerically indexed array:

<?php
$sql = "
    DECLARE @YearsToPass INT
    SET @YearsToPass = 10;

    WITH cte AS
    (
        SELECT DATEPART(YY, GETDATE())- @YearsToPass + 1 as Years
        UNION ALL
        SELECT Years + 1 as Years
        FROM cte
        WHERE Years + 1 <= YEAR(GETDATE())
    )

    SELECT cte.Years, DATEFROMPARTS(cte.Years, 12, 31),  SUM(Inv.[Nabavna vrijednost])
    FROM cte
    left join  [Drezga01].[dbo].[BI_Inventory01] Inv on Inv.[Datum knjiženja] <= DATEFROMPARTS(cte.Years, 12, 31)
    group by cte.Years, DATEFROMPARTS(cte.Years, 12, 31)
    ORDER BY cte.Years DESC
";

$query = sqlsrv_query($conn_bi, $sql);
if ($query === false ) {
    print_r(sqlsrv_errors(), true);
    exit;
}

while($row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_NUMERIC)){
    $zaliha_array[] = array(
        'godina' => $row[0],
        'zaliha' => abs($row[2])
    );
}
debugVar($zaliha_array);
?>

You have some requirements for this communication to work properly:

  1. You need to check the operating system requirements, sql server version and php version. Then you download the driver for your version of php. This link can help you with this step System requirements for the microsoft drivers

  2. Download and install the ODBC driver. You can download ODBC Driver here

  3. Make reference of the drivers that you downloaded in php.ini where the extensions are as this picture shows

    Obs: Do not forget to take the ';' before the word extension

    Obs: Do not forget that the driver files need to be in the php / ext directory which is where the php drivers are

  4. Finally use the phpinfo() function and verify that the pdo_sqlsrv extension has been loaded. he should stay like this

I hope this helps to solve the problem o/

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