sqlsrv_execute 没有 return 任何东西

sqlsrv_execute doesn't return anything

我正在使用 PHP 的 SQL 服务器驱动程序来访问 SQL 服务器数据库,但我在使用 sqlsrv_prparesqlsrv_execute 函数。

我运行两个查询:

我的代码如下所示:

$query1 = "SELECT tgt.id, src.file, src.field1 from [Table1] tgt inner join [Table2] src on tgt.id = src.id order by tgt.id";

$query2 = "UPDATE [Table1] SET field1 = ? WHERE id = ?";

$getFiles = sqlsrv_query($con, $query1); //$con is the connection with the database, received by parameter

while($row = sqlsrv_fetch_array($getFiles, SQLSRV_FETCH_BOTH)) {
  /* Some code here */
  $file = $row[1];
  $value = $row[2];
  try {
       if(!is_null($file)) {
               $stmt = sqlsrv_prepare($con, $query2, array(&$value, &$row[0]));
               if( $stmt === false ) {
                  die( print_r( sqlsrv_errors(), true));
               }
               sqlsrv_execute( $stmt );
       }
  } catch (Exception $e) {
            error_log("\nError: " . $e->getMessage());
    }
} //end while
    sqlsrv_free_stmt($getFiles);  
    sqlsrv_close($con);

问题是循环内的代码对第一行工作正常,但在第二行没有执行更新查询。 sqlsrv_prepare returns 值为 1,但 sqlsrv_execute 没有 returns 任何值。

我认为问题可能与第一次查询执行时间有关,但我不知道如何检查,考虑到没有生成错误日志,脚本一直在执行。

编辑:实际上,这个例子被简化了。将在 tgt table 上更新的值是使用 src table 中的一些数据和其他应用程序数据计算得出的。这就是我使用循环的原因,对于 query1 返回的每一行,都会计算特定值并将其用于 query2。我已经检查过这些值是否正确计算,这就是为什么我认为最好简化示例。

要解决这个问题,我必须 运行 单独查询:

  • 首先我 运行 查询 1,计算更新 tgt table 所需的数据并将它们存储在数组中;
  • 然后,使用存储在数组中的数据,我 运行 query2.

不需要其他更改。