简体   繁体   中英

sqlsrv_execute doesn't return anything

I'm using the SQL Server drivers for PHP to access a SQL Server database and I have a problem to update some data using sqlsrv_prpare and sqlsrv_execute functions.

I'm running two queries:

  • In the first query I'm retrieving some binary data (In SQL Server Management Studio, this query takes about 15 minutes to getting completed);
  • Then, for each row returned by the first query execution I'm trying to Update some data on the database.

Here's how my code looks like:

$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);

The problem is that the code inside the loop works fine to the first row, but on the second the update query isn't executed. The sqlsrv_prepare returns the value 1, but the sqlsrv_execute doesn't returns anything.

I'm thinking that the problem could be related to the first query execution time, but I don't know how to check this, considering that no error log is generated, the script just keeps executing forever.

EDIT: Actually, the example was simplified. The values that will be updated on tgt table are calculated using some data that are in src table and other application data. That's the reason why I use the loop, for each row returned by query1 specific values are calculated and used on query2. I already checked that these values are correctly calculated, this is why I thought it's better to simplify the example.

To solve this problem I have to ran the queries separately:

  • First I ran the query1, made the computation of the data that I needed to update the tgt table and stored them in an array;
  • Then, using the data stored in array, I ran the query2.

No other changes were needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM