phpsql-serverpdo

check pdo php insert success


$hpaystmt = $con->_con->prepare(".....");

object(PDOStatement)#7 (1) { ["queryString"]=> string(1515) " INSERT INTO hpay ( enccode, orno, hpercode, acctno, amt, curcode, paytype, paycode, entryby, payctr, chrgcode, itemcode, chrgtbl) VALUES ( '000060000000000101783710/14/201722:00:00', '00000000033', '000000001017837', '2017-000165903', '1500', 'PESO', 'F', 'C', '', '1', 'DR', 'DR1', 'MISC') "}====object(PDOStatement)#8 (1) { ["queryString"]=> string(1517) " INSERT INTO hpay ( enccode, orno, hpercode, acctno, amt, curcode, paytype, paycode, entryby, payctr, chrgcode, itemcode, chrgtbl) VALUES ( '000060000000000101783710/14/201722:00:00', '00000000033', '000000001017837', '2017-000165903', '100', 'PESO', 'F', 'C', '', '2', 'BIRTC', '085', 'MISC') "}

I get the above code when from

ini_set('display_errors', 1);
var_dump($hpaystmt);
$hpayinsert = $hpaystmt->execute();
echo "==";
echo $hpaystmt->execute() === TRUE ;
echo "==";

My aim is to check if the insert is success or not but echo $hpaystmt->execute() === TRUE ; this line always return blank that is why i alwats get }==== in echo;

I dont get any error from display error and also I wrap this in try catch and I always go in try part not catch.

How can I check this for success insert?

OTHER INFO:

I want to check if success so that if it fails I want to rollback the database my var $hpayinsert is set to $hpayinsert = true; before the inserting. then after i check for true or false then commit or rollback depending on the value of that var.

UPDATE:

after putting print_r($hpaystmt->errorInfo()); I get:

Array( [0] => 22001 [1] => 8152 [2] => [Microsoft][ODBC SQL Server Driver][SQL Server]String or binary data would be truncated. (SQLExecute[8152] at ext\pdo_odbc\odbc_stmt.c:254) [3] => 22001)Array( [0] => 22001 [1] => 8152 [2] => [Microsoft][ODBC SQL Server Driver][SQL Server]String or binary data would be truncated. (SQLExecute[8152] at ext\pdo_odbc\odbc_stmt.c:254) [3] => 22001)


Solution

  • You don't need it.

    There are three possible scenarios for handling the result of insert operation in PDO:

    1. To tell the success, no verification is needed. Just keep with your program flow.
    2. To handle an unexpected error, keep with the same - no immediate handling code is needed. An exception will be thrown in case of a database error, and it will bubble up to the site-wide error handler that eventually will result in a common 500 error page.
    3. To handle an expected error, like a duplicate primary key, and if you have a certain scenario to handle this very error - then use a try..catch operator.

    For a regular PHP user it sounds a bit alien - how's that, not to verify the direct result of the operation? - but this is how exceptions work - you check the error somewhere else. Once for all. Extremely convenient.

    So, in a generic case you don't need any handling code at all. Just keep your code as is.

    I wrote an article on PHP error reporting basics explaining the matter in detail, you may find it useful

    Only in case you have a handling scenario other than just reporting the error, you could catch an error. To rollback a transaction for example. The code is taken from my PDO tutorial:

    try {
        $pdo->beginTransaction();
        $stmt = $pdo->prepare("INSERT INTO users (name) VALUES (?)");
        foreach (['Joe','Ben'] as $name)
        {
            $stmt->execute([$name]);
        }
        $pdo->commit();
    }catch (Exception $e){
        $pdo->rollback();
        throw $e;
    }