J
J
Jesse Pinkman2021-06-23 18:41:38
PHP
Jesse Pinkman, 2021-06-23 18:41:38

How to catch errors when writing and updating to the database from a function?

I have two functions that perform the usual insert and update records in the database

user_insert($args); //эта функция добавляет запись в бд
user_update($args);  //эта обновляет запись в базе


I need if the first function will cause an error, so that the second function would not work.

I saw a similar solution on one English-speaking forum, but it was not marked with a solution:

$pdo->beginTransaction();
try {
  user_insert($args);
  user_update($args);
} catch (\PDOException $e) {
    $pdo->rollback();
    throw $e;
}


Or, in case of an error, the script will stop by default and all these things are not needed? Tell me please

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question