1--TEST--
2mysqli_begin_transaction()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7
8require_once('connect.inc');
9if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
10    die(sprintf("skip Cannot connect, [%d] %s", mysqli_connect_errno(), mysqli_connect_error()));
11
12if (!have_innodb($link))
13    die(sprintf("skip Needs InnoDB support, [%d] %s", $link->errno, $link->error));
14?>
15--FILE--
16<?php
17    require_once("connect.inc");
18    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
19        printf("[004] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
20            $host, $user, $db, $port, $socket);
21
22    if (!mysqli_query($link, 'DROP TABLE IF EXISTS test'))
23        printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
24
25    if (!mysqli_query($link, 'CREATE TABLE test(id INT) ENGINE = InnoDB'))
26        printf("[009] Cannot create test table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
27
28    if (true !== ($tmp = mysqli_autocommit($link, true)))
29        printf("[010] Cannot turn on autocommit, expecting true, got %s/%s\n", gettype($tmp), $tmp);
30
31    /* overrule autocommit */
32    if (true !== ($tmp = mysqli_begin_transaction($link)))
33        printf("[011] Got %s - [%d] %s\n", var_dump($tmp, true), mysqli_errno($link), mysqli_error($link));
34
35    if (!mysqli_query($link, 'INSERT INTO test(id) VALUES (1)'))
36        printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
37
38    $tmp = mysqli_rollback($link);
39    if ($tmp !== true)
40        printf("[013] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
41
42    /* empty */
43    $res = mysqli_query($link, "SELECT * FROM test");
44    var_dump($res->fetch_assoc());
45
46    /* valid flags */
47    $flags = array(MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT);
48
49    if (mysqli_get_server_version($link) >= 50605) {
50        $flags[] = MYSQLI_TRANS_START_READ_WRITE;
51        $flags[] = MYSQLI_TRANS_START_READ_ONLY;
52    }
53
54    /* just coverage */
55    foreach ($flags as $flag) {
56        if (!mysqli_begin_transaction($link, $flag, sprintf("flag %d", $flag))) {
57            printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
58        }
59        if (!mysqli_query($link, 'SELECT * FROM test') || !mysqli_rollback($link)) {
60            printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
61        }
62    }
63
64    /* does it really set a flag? */
65    if (mysqli_get_server_version($link) >= 50605) {
66        if (!mysqli_begin_transaction($link, MYSQLI_TRANS_START_READ_ONLY, sprintf("flag %d", $flag))) {
67            printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
68        }
69        if (mysqli_query($link, "INSERT INTO test(id) VALUES (2)")) {
70            printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
71        } else if (!mysqli_commit($link)) {
72            printf("[018] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
73        } else {
74            $res = mysqli_query($link, "SELECT id FROM test WHERE id = 2");
75        }
76    }
77
78    try {
79        mysqli_begin_transaction($link, -1);
80        printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
81    } catch (\ValueError $e) {
82        echo $e->getMessage() . \PHP_EOL;
83    }
84
85    if (mysqli_get_server_version($link) >= 50605) {
86        /* does it like stupid names? */
87        if (@!$link->begin_transaction(MYSQLI_TRANS_START_READ_WRITE, "*/trick me?\n\0"))
88                printf("[020] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
89
90        /* does it like stupid names? */
91        if (@!$link->begin_transaction(MYSQLI_TRANS_START_READ_WRITE, "az09"))
92            printf("[021] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
93    }
94
95    print "done!";
96?>
97--CLEAN--
98<?php
99    require_once("clean_table.inc");
100?>
101--EXPECT--
102NULL
103mysqli_begin_transaction(): Argument #2 ($flags) must be one of the MYSQLI_TRANS_* constants
104done!
105