PHP How to handle errors if multiple sqlsrv requests are sent simultaneously

I have the following table:

CREATE TABLE [dbo].[TestDB] (
    [col1] INT NOT NULL, 
    [col2] VARCHAR(50) NOT NULL

);

Now I want to add entries:

    $params=array(123,'someString');
    $sql = "insert into testDB values(?,?)";

    $stmt = sqlsrv_query( $conntask, $sql ,$params);
    if( $stmt === false) {
        echo $sql;
        print_r($params);
        die( print_r( sqlsrv_errors(), true) );
    }

If an error occurs, $ stmt will be false, a message will be printed, and the script will complete.

My problem

If I want to add multiple entries, I send all requests at once.

    $params=array(123,'someString','notANumber','someOtherString');
    $sql = "insert into testDB values(?,?) insert into testDB values(?,?)";

    $stmt = sqlsrv_query( $conntask, $sql ,$params);
    if( $stmt === false) {
        echo $sql;
        print_r($params);
        die( print_r( sqlsrv_errors(), true) );
    }

In this example, the first insert will succeed and the second will fail because I am trying to put a row in an int column.
Now $ stmt is not false, and the request is executed until an error occurs.

My questions:

  • How to check if the request failed at any time?
  • How can I make sure the request is fully executed or not?
+4
source share
1

.

SET XACT_ABORT ON , BEGIN TRANSACTION .

  • SMSS

    SET XACT_ABORT ON;
    
    BEGIN TRANSACTION
      -- your queries here
    COMMIT TRANSACTION
    
  • PHP

    ...
    $sql = "SET XACT_ABORT ON; " .
           "BEGIN TRANSACTION " .
           "insert into testDB values(?,?) insert into testDB values(?,?)" .
           "COMMIT TRANSACTION";
    ...
    
+2

All Articles