postgresql php transaction(保證資料庫多筆存取正確性-不會發生一筆正確其他失敗)
postgresql php transaction(保證資料庫多筆存取正確性-不會發生一筆正確其他失敗)
<?php
$host = “localhost”;
$user = “user12”;
$pass = “34klq*”;
$db = “testdb”;
$con = pg_connect(“host=$host dbname=$db user=$user password=$pass”)
or die (“Could not connect to server\n”);
pg_query(“BEGIN”) or die(“Could not start transaction\n”);
$res1 = pg_query(“DELETE FROM cars WHERE id IN (1, 9)”);
$res2 = pg_query(“INSERT INTO cars VALUES (1, ‘BMW’, 36000), (9, ‘Audi’, 52642)”);
if ($res1 and $res2) {
echo “Commiting transaction\n”;
pg_query(“COMMIT”) or die(“Transaction commit failed\n”);
} else {
echo “Rolling back transaction\n”;
pg_query(“ROLLBACK”) or die(“Transaction rollback failed\n”);;
}
pg_close($con);
?>