Guest
Public paste!

zingbats

By: a guest | Feb 9th, 2010 | Syntax: PHP | Size: 0.81 KB | Hits: 20 | Expires: Never
Copy text to clipboard
  1. <?php
  2. query("START TRANSACTION");
  3. //
  4. // Check there are enough bottles of wine left in the `items` table
  5. //
  6. $bottles_left = query("SELECT `number` FROM `items` WHERE `type` = 'wine' LIMIT 1 FOR UPDATE;");
  7. //
  8. // If we have bottles left
  9. //
  10. if( $bottles_left > 0) {
  11.         //
  12.         // Call a function that returns TRUE on WIN
  13.         // This simulates a competition
  14.         //
  15.         $did_we_win = generate_win();
  16.         if( $did_we_win ) {
  17.                 //
  18.                 // Rollback at the first sign of error
  19.                 //
  20.                 query("UPDATE `users` SET `prizes_won`= prizes_won+1 WHERE `user_id` = $UID") or query("ROLLBACK");
  21.                
  22.                 query("UPDATE `items` SET `number` = number-1 WHERE `type` = 'wine' LIMIT `;") or query("ROLLBACK");
  23.                 //
  24.                 // We have done everything, so we commit
  25.                 //
  26.                 query("COMMIT");
  27.         } else {
  28.                 query("COMMIT");
  29.         }
  30. } else {
  31.         query("COMMIT");
  32. }
  33. ?>