I'm trying to execute an SQL procedure and return values in php. My code is as below :
<?PHP
include "conn.php";
$myquery= "exec CR_Report @System='Automated Forms'";
$fetched=sqlsrv_query($conn,$myquery);
if( $fetched === false ) { die( print_r( sqlsrv_errors(), true ));}
while($res=sqlsrv_fetch_array($fetched,SQLSRV_FETCH_ASSOC))
{
echo $res['ID'];
}
?>
Procedure :
CREATE procedure [dbo].[CR_Report]
@Department varchar(60) = Null,
@System varchar(30) = Null,
@Fromdate date = Null,
@Todate date = Null
AS
IF @System is Null and
@Fromdate is Null and
@Todate is Null
BEGIN
Create table #Temp_ConfigRequest_Department
(
ID int,
Form_No varchar(25),
App_Date smalldatetime,
System varchar(30)
)
delete #Temp_ConfigRequest_Department
insert into #Temp_ConfigRequest_Department
SELECT * FROM [SUAF].[dbo].[Config_Request] where Admin_Department = @Department
select * from #Temp_ConfigRequest_Department
DROP TABLE #Temp_ConfigRequest_Department
end
IF @Department is Null and
@Fromdate is Null and
@Todate is Null
BEGIN
Create table #Temp_ConfigRequest_System
(
ID int,
Form_No varchar(25),
App_Date smalldatetime,
System varchar(30)
)
delete #Temp_ConfigRequest_System
insert into #Temp_ConfigRequest_System
SELECT * FROM [SUAF].[dbo].[Config_Request] where System = @System
select * from #Temp_ConfigRequest_System
DROP TABLE #Temp_ConfigRequest_System
end
IF @Department is NULL and
@System is NULL
BEGIN
Create table #Temp_ConfigRequest_Date
(
ID int,
Form_No varchar(25),
App_Date smalldatetime,
System varchar(30)
)
delete #Temp_ConfigRequest_Date
insert into #Temp_ConfigRequest_Date
SELECT * FROM [SUAF].[dbo].[Config_Request] where convert(date, App_Date, 103) Between @Fromdate and @Todate
select * from #Temp_ConfigRequest_Date
DROP TABLE #Temp_ConfigRequest_Date
end
IF @Department is not Null and
@System is not Null and
@Fromdate is not Null and
@Todate is not Null
BEGIN
Create table #Temp_ConfigRequest_All
(
ID int,
Form_No varchar(25),
App_Date smalldatetime,
System varchar(30)
)
delete #Temp_ConfigRequest_All
insert into #Temp_ConfigRequest_All
SELECT * FROM [SUAF].[dbo].[Config_Request] where convert(date, App_Date, 103) Between @Fromdate and @Todate and Admin_Department = @Department and System = @System
select * from #Temp_ConfigRequest_All
DROP TABLE #Temp_ConfigRequest_All
end
It doesn't return any value and also no errors are logged in the error log. I tried testing the sql query in the database and it does return record. To test the connection, I wrote a code for another procedure in the same file, and it is returning values. The second code is as below :
<?PHP
$myquery="exec Configuration_Request @IDtemp='4275'";
$fetched=sqlsrv_query($conn,$myquery) ;
if( $fetched === false ) { die( print_r( sqlsrv_errors(), true ));}
while($res=sqlsrv_fetch_array($fetched,SQLSRV_FETCH_ASSOC))
{
echo $res['Form_No'];
echo $res['Administrator'];
}
?>
Procedure :
create procedure Configuration_Request (@IDtemp int)
as
begin
select * from Config_Request where ID=@IDtemp
end
Where is the issue? Why is the 1st code not returning any values? Would appreciate any help/suggestion.