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