xref: /PHP-5.4/ext/pdo/tests/pdo_017.phpt (revision be0793d2)
1--TEST--
2PDO Common: transactions
3--SKIPIF--
4<?php # vim:ft=php
5if (!extension_loaded('pdo')) die('skip');
6$dir = getenv('REDIR_TEST_DIR');
7if (false == $dir) die('skip no driver');
8require_once $dir . 'pdo_test.inc';
9PDOTest::skip();
10
11$db = PDOTest::factory();
12try {
13  $db->beginTransaction();
14} catch (PDOException $e) {
15  die('skip no working transactions: ' . $e->getMessage());
16}
17
18if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') {
19	require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
20	if (false === MySQLPDOTest::detect_transactional_mysql_engine($db)) {
21		die('skip your mysql configuration does not support working transactions');
22	}
23}
24?>
25--FILE--
26<?php
27if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.dirname(__FILE__) . '/../../pdo/tests/');
28require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
29$db = PDOTest::factory();
30
31if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') {
32	require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
33	$suf = ' ENGINE=' . MySQLPDOTest::detect_transactional_mysql_engine($db);
34} else {
35	$suf = '';
36}
37
38$db->exec('CREATE TABLE test(id INT NOT NULL PRIMARY KEY, val VARCHAR(10))'.$suf);
39$db->exec("INSERT INTO test VALUES(1, 'A')");
40$db->exec("INSERT INTO test VALUES(2, 'B')");
41$db->exec("INSERT INTO test VALUES(3, 'C')");
42$delete = $db->prepare('DELETE FROM test');
43
44function countRows($action) {
45    global $db;
46	$select = $db->prepare('SELECT COUNT(*) FROM test');
47	$select->execute();
48    $res = $select->fetchColumn();
49    return "Counted $res rows after $action.\n";
50}
51
52echo countRows('insert');
53
54$db->beginTransaction();
55$delete->execute();
56echo countRows('delete');
57$db->rollBack();
58
59echo countRows('rollback');
60
61$db->beginTransaction();
62$delete->execute();
63echo countRows('delete');
64$db->commit();
65
66echo countRows('commit');
67
68?>
69--EXPECT--
70Counted 3 rows after insert.
71Counted 0 rows after delete.
72Counted 3 rows after rollback.
73Counted 0 rows after delete.
74Counted 0 rows after commit.
75