php - Catch Duplicate entry with prepared SQL statement -


i want handle "duplicate entry" error prepared mysql statement.

i have thoses lines

$result = $db->prepare("insert comptability (id_comptability, order_id, reduction, `%tva`, facture) values (?, ?, ?, ?, ?)"); $result->execute(array($no_facture, $id_order, $reduc_tot, $tot, $date)); 

and want execute code if there not error. , display pop-up , make prog die if there error.

i tried

if($result === false)//catch error {} else //it worked {} 

but did not worked

you can use insert ignore , check how many rows affected using pdostatement::rowcount

concept should along lines of:

$result = $db->prepare("insert ignore comptability (id_comptability, order_id, reduction, `%tva`, facture) values (?, ?, ?, ?, ?)"); if ($result->execute(array($no_facture, $id_order, $reduc_tot, $tot, $date))) {   if ($result->rowcount() == 0) {    // had duplicate record.   } else {    // good.   } } 

Comments