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?
$query = sqlsrv_query($conn_bi, $sql); if ($query === false ) {print_r(sqlsrv_errors(), true); exit;}. Thanks.