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