使用 SQL 服务器和 PHP 显示存储过程的结果

Display results from Stored Procedure using SQL Server and PHP

我正在尝试使用 PHP 和 SQL 服务器显示存储过程的输出。存储过程是一个 SELECT 语句。截至目前,我最终处于显示成功消息的 else 条件,但我无法显示查询结果。这是函数:

  function account_search_sp($account_number,$occupant_code,$name,$address,$bill_code,$utility_code){
   global $db;

   sqlsrv_configure("WarningsReturnAsErrors", 0);

   $sql = "EXEC sp_cigar_account_search @AcctNo= ?, @OccupantCode= ?, @Name= ?, @Address= ?, @BillCode= ?, @UtilityType= ?";

   $procedure_params = array(
   array(&$account_number, SQLSRV_PARAM_IN),
   array(&$occupant_code, SQLSRV_PARAM_IN),
   array(&$name, SQLSRV_PARAM_IN),
   array(&$address, SQLSRV_PARAM_IN),
   array(&$bill_code, SQLSRV_PARAM_IN),
   array(&$utility_code, SQLSRV_PARAM_IN)
   );

   $stmt = sqlsrv_prepare($db, $sql, $procedure_params);

   $result = sqlsrv_execute($stmt);

   if( !$result ) {
     //Show errors
     echo "Die error <br>";
     die( print_r( sqlsrv_errors(), true));

   }else{
     echo "<br><h3>Success</h3><br>";
     sqlsrv_next_result($stmt);
   }

return $stmt; }

如果我 运行 SSMS 中的存储过程例如使用 200 作为帐号,我会取回数据。

我正在使用 PHP 7.4

感谢任何帮助。如果我需要包含更多信息,请告诉我。

如果我理解正确,您需要使用 sqlsrv_ferch_array()sqlsrv_fetch_object():

获取数据
<?
function account_search_sp($account_number, $occupant_code, $name, $address, $bill_code, $utility_code) {
   global $db;

   sqlsrv_configure("WarningsReturnAsErrors", 0);

   $sql = "EXEC sp_cigar_account_search @AcctNo= ?, @OccupantCode= ?, @Name= ?, @Address= ?, @BillCode= ?, @UtilityType= ?";

   $procedure_params = array(
      array(&$account_number, SQLSRV_PARAM_IN),
      array(&$occupant_code, SQLSRV_PARAM_IN),
      array(&$name, SQLSRV_PARAM_IN),
      array(&$address, SQLSRV_PARAM_IN),
      array(&$bill_code, SQLSRV_PARAM_IN),
      array(&$utility_code, SQLSRV_PARAM_IN)
   );

   $stmt = sqlsrv_prepare($db, $sql, $procedure_params);
   if ($stmt === false) {
      echo "Die error <br>";
      die( print_r( sqlsrv_errors(), true));
   }
   
   if (sqlsrv_execute($stmt) === false) {
      echo "Die error <br>";
      die( print_r( sqlsrv_errors(), true));
   }
   
   echo "<br><h3>Success</h3><br>";
   $data = array();
   do {
      while ($row = sqlsrv_fetch_array($stmt)) {
         $data[] = $row;
      }
   } while (sqlsrv_next_result($stmt)); 

   return $data; 
}
?>