0

I have a stored proc in mssql. The SP works fine while executed directly. When I call it in PHP (PHP Version 8.1.2) I get no result.

The error in browser is:

Array ( [0] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 16954 [code] => 16954 [2] => [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Executing SQL directly; no cursor. [message] => [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Executing SQL directly; no cursor. )

)

PHP code and I use user sa with password.

<?php  

include "SQLconnectTposApp.php";
header("Content-type:application/json;charset=utf-8"); 
 
if( $conn ) {
    
     $sql =   " EXECUTE SpActionCasa  @date1=? , @date2=? , @userNo=? , @Bno =?" ; 
     $params  = array("01/01/2015", "07/07/2023", 0 ,0 );
     $options = array("Scrollable" => SQLSRV_CURSOR_KEYSET  );
     $stmt = sqlsrv_query( $conn, $sql , $params, $options );
     if( $stmt === false ) {
        die( print_r( sqlsrv_errors(), true));
     }
     $row_count = sqlsrv_num_rows( $stmt );
     $err =  array(
                'Erad'=> '0.0' ,
                'Masrof'=> '0.0' , 
                'UserDiscrption'=> ' لايوجد بيانات ' , 
                'gdate'=> array('date'=>'2022-01-01 00:00:00.000000', 'timezone_type' => 3,'timezone' => 'UTC') , 
                'doctype'=> 'لايوجد بيانات'  
     );
     if ($row_count === false)
     { 
        echo " row_count === false ";
        $json = array_fill(0, 1, $err); 
     }
     else
     {
        if ($row_count === 0) 
        {
           echo "row_count 0";
           $json =  array_fill(0, 1, $err); 
        }
        else
        {
           $json = array();
            while( $row = sqlsrv_fetch_array( $stmt , SQLSRV_FETCH_ASSOC) ) 
            {
               array_push($json ,$row);        
            } 
        }    
        echo json_encode($json); 
     } 
} else {
     echo " Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}  
    
sqlsrv_free_stmt($stmt); 
sqlsrv_close($conn); //Close the connnection first

?> 

This is the stored procedure. The problem is in #temp Table it work in php api when i remove the temp table

ALTER PROCEDURE [dbo].[SpActionCasa] 
@date1 datetime,@date2 datetime , @userNo int, @Bno int
 
AS
BEGIN

create table #TempActCasa(
   gdate datetime NULL ,
   daen numeric(18,3) NULL,
   maden numeric(18,3) NULL,
   rased numeric(18,3) NULL,
   vWhy nvarchar(170) NULL,
   idnum int NULL,
   flg smallint NULL,
   vtype nvarchar(25) NULL,
   userNo int NULL,
   optime Datetime NULL 
)
   
    
insert into #TempActCasa (gdate, daen, maden,rased,vWhy,idnum,vtype, userNo , optime)
SELECT wasDate, WasValue, 0, 0, CostName+' '+wassabb , WasNo , 'ايصال دفع'   , userNo , operation_time 
FROM TWAS 
left join TCostumer on (TWAS.WasCostNo = TCostumer.CostNo)
WHERE 
   (FType = 0) and
   (BankNo = @Bno) and
   ((wasDate >= @date1) and (wasDate <= @date2) and (userNo = ISNULL(@userNO,userno)))

insert into #TempActCasa(gdate,daen,maden,rased,vWhy , idnum, vtype, userNo , optime)
SELECT GDATE, GValue, 0, 0, sarftypeName + '  ' + sarfto, AutoNo , 'مصروفات' , userNo , operation_time 
FRom TMasrof 
left join dbo.Lsarftype on (TMasrof.sarftypeNo = Lsarftype.sarftypeNo )
   
Select * 
From #TempActCasa 
 
END

Thank you for help me!

Zhorov
  • 28,486
  • 6
  • 27
  • 52
  • What is the result if you use unambiguous datetime format - `"20150101"` instead of `"01/01/2015"`? – Zhorov Sep 01 '22 at 08:07
  • 2
    You should likely also put `SET NOCOUNT ON;` at the start of the procedure. – Thom A Sep 01 '22 at 08:13
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Sep 01 '22 at 08:26
  • Why does it need a temp table, why not just do `SELECT ... UNION ALL SELECT ...`? Also yeah PHP can run into problems if you don't use `SET NOCOUNT ON`. Also use a proper date value `new DateTime('2022-01-01 00:00:00.000000', UTC)` – Charlieface Sep 01 '22 at 11:13
  • Thank you All I put `SET NOCOUNT ON;` still get the same error , I know `union` but I must use TempTable – Walid Jmail Sep 02 '22 at 08:43

0 Answers0