zingbats
By: a guest | Feb 9th, 2010 | Syntax:
PHP | Size: 0.81 KB | Hits: 20 | Expires: Never
<?php
query("START TRANSACTION");
//
// Check there are enough bottles of wine left in the `items` table
//
$bottles_left = query("SELECT `number` FROM `items` WHERE `type` = 'wine' LIMIT 1 FOR UPDATE;");
//
// If we have bottles left
//
if( $bottles_left > 0) {
//
// Call a function that returns TRUE on WIN
// This simulates a competition
//
$did_we_win = generate_win();
if( $did_we_win ) {
//
// Rollback at the first sign of error
//
query("UPDATE `users` SET `prizes_won`= prizes_won+1 WHERE `user_id` = $UID") or query("ROLLBACK");
query("UPDATE `items` SET `number` = number-1 WHERE `type` = 'wine' LIMIT `;") or query("ROLLBACK");
//
// We have done everything, so we commit
//
query("COMMIT");
} else {
query("COMMIT");
}
} else {
query("COMMIT");
}
?>