Try Catch does not work in PHP with PDO

0

I have the following block of code to execute an insert, the insert is throwing me an error that a field that I do not include in my insert does not have a default value (use the $ stmt-> gtInfoInfo command).

The Code is as follows:

try {
        $db=SPDO::singleton();
        $stmt=$db->prepare("insert into TB_CHEQUE(iChq_Ide,iChq_Cta,sChq_Num,iChq_Uso,dChq_Fem,dChq_Fco,iChq_IdeRes,iChq_IdeRef,dChq_Imp,dChq_Sal,iChq_Emi,sChq_Est) values(Null,?,?,?,?,?,?,?,?,?,?,'V');");
        $stmt->bindParam(1,$iChq_Cta,PDO::PARAM_INT);
        $stmt->bindParam(2,$sChq_Num,PDO::PARAM_STR,12);
        $stmt->bindParam(3,$iChq_Uso,PDO::PARAM_INT);
        $fecha = DateTime::createFromFormat('d/m/Y',$dChq_Fem);
        $fecha = $fecha->format('Y-m-d');
        $stmt->bindParam(4,$fecha,PDO::PARAM_STR);
        $fecha = DateTime::createFromFormat('d/m/Y',$dChq_Fco);
        $fecha = $fecha->format('Y-m-d');
        $stmt->bindParam(5,$fecha,PDO::PARAM_STR);
        $stmt->bindParam(6,$iChq_IdeRes,PDO::PARAM_INT);
        $stmt->bindParam(7,$iChq_IdeRef,PDO::PARAM_INT);
        $stmt->bindParam(8,$dChq_Imp,PDO::PARAM_INT);
        $stmt->bindParam(9,$dChq_Imp,PDO::PARAM_INT);
        $stmt->bindParam(10,$iChq_Emi,PDO::PARAM_INT);
        $stmt->execute();

    } catch (PDOException $e) {
        echo 'Falló la Ejecución: ' . $e->getMessage();
    }

Why TryCatch does not work ?, using the ($ stmt-> errorInfo) I can detect the error but the truth is doing it every time I execute a statement in the database it does not seem the most appropriate, thanks for your time.

Adding info: when I say that it does not work I mean that it does not show any errors, in that insert I am missing some fields that do not have default values (use the instruction $ stmt-> gtInfoInfo). it is assumed that the try catch should capture the error and display it.

    
asked by Emersoft 25.10.2017 в 19:38
source

1 answer

1

The default method for handling PDO errors is PDO::ERRMODE_SILENT , which you never get will throw an exception. You have to define PDO::ERRMODE_EXCEPTION mode yourself when opening the connection.

I do not know what's in your singleton but you should be able to do it with:

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
answered by 25.10.2017 / 20:08
source