S
S
softingeneer2018-06-13 17:29:07
WordPress
softingeneer, 2018-06-13 17:29:07

How do transactions work in Wordpress?

Good afternoon everyone, how to work with transactions in Wordpress, I use the following code, attention - the commit is commented out:

try {
    $wpdb->query('START TRANSACTION');
    $result = $wpdb->query('TRUNCATE TABLE wp_competition_history');
    if (!$result) {
       throw new Exception("Ошибка БД");
    }
    //$wpdb->query('COMMIT');
} catch (Exception $ex) {
        $wpdb->query('ROLLBACK');
    }

In this example, the commit was not supposed to work, yet it does work as if the transactions weren't working. Tell me, please, how to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Kozlov, 2018-06-16
@trampick

It's not clear whether you defined global $wpdb somewhere earlier;
Try replacing the condition with this

if (false === $result) {
throw new Exception("Ошибка БД. ".$wpdb->last_error);
}

I think it's a matter of conditions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question