Heim php教程 PHP源码 PHP简单的事务测试用例

PHP简单的事务测试用例

May 26, 2016 am 08:18 AM


 show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
// schemal
create table transaction_tbl (
	name char(10) not null
) engine=innodb;

*/

class Transaction 
{
	// 判断插入查询是否成功
	private $error = 0;
	
	// 连接资源
	private $conn_id = null;
	
	public function __construct()
	{
		$this->connect();
	}
	
	public function test()
	{
		echo "BEGIN TEST\n";

		//开始事务
		$this->beginTransaction();
		// 插入测试数据
		// name 的大小限制为10
		$str1 = str_repeat("a", mt_rand(1, 13));
		$str2 = str_repeat("b", mt_rand(1, 13));

		$this->query("INSERT INTO `transaction_tbl` (`name`) VALUES ('$str1')");
		$this->query("INSERT INTO `transaction_tbl` (`name`) VALUES ('$str2')");			
		
		//提交查询
		if ($this->error == 0)
		{
			$this->commit();
		}
		else
		{
			$this->rollback();
		}
		
		echo "END TEST\n";
	}
	
	private function query($sql)
	{
		echo $sql."\n";
		(mysql_query($sql, $this->conn_id) == FALSE) && ++$this->error;
	}
	
	// 开始事务
	private function beginTransaction()
	{
		mysql_query("BEGIN", $this->conn_id);
	}
	//回滚
	private function rollback()
	{
		echo "ROLLBACK\n";
		mysql_query("ROLLBACK", $this->conn_id);	
	}
	//提交
	private function commit()
	{
		echo "save done\n";
		mysql_query("COMMIT", $this->conn_id);
	}
	
	// 连接数据库
	private function connect()
	{
		$this->conn_id = @mysql_connect("127.0.0.1", "root", "") or die("can\'t connect: ". mysql_error());
		mysql_select_db("test", $this->conn_id) or die("can\'t use test: ". mysql_error());
	}
	// 关闭数据
	private function close()
	{
		mysql_close($this->conn_id);
	}	
	
	public function __destruct()
	{
		$this->close();
	}		
}

$trans = new Transaction();
$trans->test();
Nach dem Login kopieren

                   

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Repo: Wie man Teamkollegen wiederbelebt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Abenteuer: Wie man riesige Samen bekommt
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)