Mysql transaction using PDO

Executes an array of SQLs as a single transaction using PDO. Read more.

php
fork add to bundle source
function mysql_transaction($sqls) { $pdo = new PDO('mysql:host=127.0.0.1;dbname=db', 'user', 'pwd'); $pdo->beginTransaction(); foreach ( $sqls as $sql ) { $pdo->exec($sql); if ( $pdo->errorInfo()[1] ) { $pdo->rollback(); return false; } } $pdo->commit(); return true; }

usage

load_function('mysql_transaction'); mysql_transaction([ "INSERT INTO users SET email = 'anyone@function.codes'", "INSERT INTO users_properties SET email = 'anyone@function.codes'", "..." ]); // Will execute each query within a single transaction and return "true" on success
← how to use this in app?
create log ...inout