正在 php laravel 中执行存储过程

Executing stored procedure in php laravel

我正在使用 Laravel 7,我想显示存储过程的结果。我的代码如下。当我在 SQL 服务器中执行带有参数的存储过程时,它正在显示数据。但在 Laravel 应用程序数据未显示。

请帮我找出问题。

$serverName = env("DB_HOST");
$connectionInfo = array( "Database"=>env("DB_DATABASE"), "UID"=>env("DB_USERNAME"), "PWD"=>env("DB_PASSWORD") );
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
  die( print_r( sqlsrv_errors(), true));
}
$tsql= " EXEC USP_Daily_TA_Punching_Detailswith_OT '$employee','$datefrom','$dateto'";
$getResults= sqlsrv_query($conn, $tsql);
      $data = array();
      if ($getResults == FALSE)
      {
      echo '';
      }
      else {
       //$data[] ='';
      do
      {
        while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC))
        {
         $data[] = $row;
        }
      }
       while (sqlsrv_next_result($getResults));
      }
      if(count($data)>0){
       sqlsrv_free_stmt($getResults);
      $total_row = count($data);
     }

尝试以下方法调用 laravel

中的存储过程
DB::select("call USP_Daily_TA_Punching_Detailswith_OT('".$employee."','".$datefrom."','".$dateto."')");

始终尝试在语句中使用参数以防止可能的 SQL 注入问题。作为附加说明,当您将日期值传递给 SQL 服务器时,请使用明确的日期格式:

示例使用 PHP 驱动程序 SQL 服务器:

<?php
// Connection
$serverName = env("DB_HOST");
$connectionInfo = array(
    "Database"=>env("DB_DATABASE"), 
    "UID"=>env("DB_USERNAME"), 
    "PWD"=>env("DB_PASSWORD")
);
$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($conn === false) {
    die(print_r(sqlsrv_errors(), true));
}

// Statement
$employee = '000010993';
$datefrom = '20200601'; 
$dateto   = '20200610';
$tsql     = "EXEC USP_Daily_TA_Punching_Detailswith_OT ?, ?, ?";
$params   = array($employee, $datefrom, $dateto);
$getResults = sqlsrv_query($conn, $tsql, $params);
if ($getResults === false) {
    die(print_r(sqlsrv_errors(), true));
}   

// Results
$data = array();
do {
    while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
        $data[] = $row;
    }
} while (sqlsrv_next_result($getResults));

// End
sqlsrv_free_stmt($getResults);
sqlsrv_close($conn);
$total_row = count($data);
?>   

使用 Laravel 的示例:

<?php

...
$employee = '000010993';
$datefrom = '20200601'; 
$dateto   = '20200610';
DB::select("SET NOCOUNT ON; EXEC USP_Daily_TA_Punching_Detailswith_OT ?, ?, ?", array($employee, $datefrom, $dateto));

...
?>