$db
mysqliの下にリストされているデータベースがあります。このデータベースは2つのテーブルに含まれており、以下にtable
andとしてリストしましたtable2
(この例のためだけに)。Table2の行には、テーブルのIDが必要です。これは問題ありませんが、テーブル2に列を追加する際に問題が発生する可能性があるため、ロールバックルーチンが必要になります。ただし、機能していないようです。
自動コミットをオフにすることから始めました。die
次に、コマンドを使用して失敗を通知しているにもかかわらず、rollbackコマンドを挿入しようとしました。私に関する限り、トランザクションは操作の途中で忘却に追いやられる可能性があり、データベースはまだ安定しているはずです。したがって、データベースが自動コミットをオフにしようとしているという事実を完全に無視していない限り、ここで何が起こっているのかわかりません。
私のコードの基本構造は以下のとおりです。
function problem($str)
{
global $db;
mysqli_rollback($db);
die($str);
}
mysqli_autocommit($db,false);
//Basic check if exists
$sqlstr = "SELECT * FROM table WHERE name = '$name';";
$r = mysqli_query($db,$sqlstr);
if (mysqli_num_rows($r)>0){problem("A row already exists under that id");}
//Insert the row
$sqlstr = "INSERT INTO table (name,v1,v2,v3) VALUES ('$name','$v1','$v2','$v3');";
$r = mysqli_query($db,$sqlstr);
if (!$r){problem("Could not insert into the table. $sqlstr");}
//Get the generated id part 1
$sqlstr = "SELECT id FROM table WHERE name = '$name';";
$r = mysqli_query($db,$sqlstr);
if (!$r){problem("Could not add into the table. $sqlstr");}
//Get the generated id part 2
$row = mysqli_fetch_assoc($r);
$eid = $row['id'];
//A simple loop
$count = count($questions);
for ($i=1;i<=$count;$i++)
{
//This is where it typically could die.
$r = mysqli_query($db,"INSERT INTO table2 VALUES (...);");
if (!$r){problem("Could not add to the table2. $sqlstr");}
}
mysqli_commit($db);
足りないものはありますか?自動コミットで見つけた例にできるだけ忠実に従うようにしました。