We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

PHP and its autocommit

Does anyone have a code for manual transactions? I've searched and searched, but I always find the same for it (this is what I have):

try
        {
            $bd = new PDO('mysql:host=localhost;charset=utf8', $argv[2], $argv[3],
                array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
            );
        }
        catch (PDOException $e)
        {
            throw new Exception('conexión con MySQL.', 2);
        }
        try
        {
            $archivos = array('tablas', 'procedimientoss', 'triggers');

            $bd->beginTransaction();

            $bd->exec('CREATE DATABASE IF NOT EXISTS ' . $argv[1] . ' DEFAULT CHARACTER SET utf8 COLLATE utf8_spanish_ci');
            $bd->exec('USE ' . $argv[1]);
            foreach ($archivos as $archivo)
            {
                $bd->exec(file_get_contents('../bd/' . $archivo . '.sql'));
            }

            $bd->commit();
        }
        catch (PDOException $e)
        {
            $bd->rollBack();
            throw new Exception('instalacion de la base de datos', 2);
        }

But beginTransaction() doesn't disable MySQL autocommit: even setting array(PDO::ATTR_AUTOCOMMIT => false (or 0)). Any help?



33.8k
edited Aug '14

I tried that the other day, but doesn't work.

Thanks anyway.



33.8k
Accepted
answer

I'm a maximun genius ... I wanted to do tests with changing values in $archivos... the fact is that it only do a not getting the contents of the file (so I were doing a $bd->exec("")), instead of throwing a PDOException... so always the transaction was successful.

poker_face