• The DATETIME column type stores the date and the time.
  • Interesting Article about using GUIDs as primary keys in MySQL
    • %JOHN%Interesting, I agree. I'm not convinced.

How to increment a counter atomically / thread safe-ly

(see common/Constants.php)
   function increment_value($name) // 'backup' always false
   {
      // strangely, returns 'true' whether it worked or not?!?
      // NOTE: this only works with MySQL.  Need some other way for
      //  atomic counter increments with other DBs
      $result =
         mysql_query("UPDATE Constants ".
         "SET tValue = LAST_INSERT_ID(tValue + 1) WHERE sName = '".$name."'");

      $result = mysql_query("SELECT LAST_INSERT_ID()");
      $row = mysql_fetch_row($result);
      $result = $row[0];

      // the only way to tell an error happened seems to be that it 
      //  returns '0' as a result (no exception is thrown).  So, if we
      //  have an error, just make a new row!
      if ($result[0] == 0)
      {
         $result = mysql_query("INSERT INTO Constants (sName, tValue," .
            " dAdded) VALUES('".$name."', 0, NOW())");
         log_debug("creating new constant for '".$name."'");
         return increment_value($name);
      }
      else   // success
      {  return $row[0];
      }
   }

-- MattWalsh - 30 Jan 2007

Topic revision: r4 - 17 Apr 2007 - MattWalsh
 
This site is powered by the TWiki collaboration platformCopyright © 2008-2012 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback