MYSQL νŠΈλžœμ μ…˜

2007/01/14 02:01

 
This is a very useful "mis-feature" (bug?) because it allows you to create something like this:

class Transaction {
  var $dbh;

  function Transaction($host, $username, $password) {
   $this->dbh = mysql_connect($host, $username, $password);
  }

  function _Transaction() {
     mysql_disconnect($this->dbh);
  }

  function begin() {
   mysql_query("BEGIN", $this->dbh);
  }

  function rollback() {
     mysql_query("ROLLBACK", $this->dbh);
  }

  function commit() {
   mysql_query("COMMIT", $this->dbh);
  }
}

which you could use to wrap around transactional statements like this:

$tx =& new Transaction("localhost", "username", "password");
$tx->begin();
$dbh = mysql_connect("localhost", "username", "password");
$result = mysql_query("INSERT ...");
if (!$result) {
  $tx->rollback();
} else {
  $tx->commit();
}
mysql_disconnect($dbh);
unset($tx);

The benefit of such a Transaction class is that it is generic and can wrap around any of your MySQL statements.

http://kr.php.net/manual/kr/ref.mysql.php