Archive for the ‘Mysql’ Category

Transacties in mysql 4.0 en php

Monday, March 20th, 2006

Transactiesupport zit in Mysql sinds versie 4.0 (werkt alleen bij tabellen van het type ‘InnoDB’). Bij een applicatie die hiërarchische data verwerkt eigenlijk noodzakelijk. Eén update statement fout en de hele structuur is weg.

De php functie accepteert een reeks sql statements en voert ze 1 voor 1 uit. Als er iets mis gaat wordt een rollback uitgevoerd.


function query($qrs)
{
if (!is_array($qrs) or sizeof($qrs) == 0)
{
return false;
}
array_unshift($qrs, "START TRANSACTION");
$ok = false;
$i = 0;
do {
$ok = mysql_query($qrs[$i]);
++$i;
} while ($ok === true && $i < sizeof($qrs));

if ($ok === true) {
mysql_query("COMMIT");
return true;
}
else
{
mysql_query("ROLLBACK");
return false;
}
}