xref: /PHP-8.1/ext/mysqli/tests/mysqli_commit.phpt (revision b5a14e6c)
1--TEST--
2mysqli_commit()
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
20    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
21        printf("[004] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
22            $host, $user, $db, $port, $socket);
23
24    if (true !== ($tmp = mysqli_autocommit($link, false)))
25        printf("[005] Cannot turn off autocommit, expecting true, got %s/%s\n", gettype($tmp), $tmp);
26
27    if (!mysqli_query($link, 'DROP TABLE IF EXISTS test'))
28        printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
29
30    if (!mysqli_query($link, 'CREATE TABLE test(id INT) ENGINE = InnoDB'))
31        printf("[007] Cannot create test table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
32
33    if (!mysqli_query($link, 'INSERT INTO test(id) VALUES (1)'))
34        printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
35
36    $tmp = mysqli_commit($link);
37    if ($tmp !== true)
38        printf("[009] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
39
40    if (!mysqli_query($link, 'ROLLBACK'))
41        printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
42
43    if (!$res = mysqli_query($link, 'SELECT COUNT(*) AS num FROM test'))
44        printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
45    $tmp = mysqli_fetch_assoc($res);
46    if (1 != $tmp['num'])
47        printf("[12] Expecting 1 row in table test, found %d rows\n", $tmp['num']);
48    mysqli_free_result($res);
49
50    if (!mysqli_query($link, 'DROP TABLE IF EXISTS test'))
51        printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
52
53    mysqli_close($link);
54
55    try {
56        mysqli_commit($link);
57    } catch (Error $exception) {
58        echo $exception->getMessage() . "\n";
59    }
60
61    print "done!";
62?>
63--CLEAN--
64<?php
65    require_once("clean_table.inc");
66?>
67--EXPECT--
68mysqli object is already closed
69done!
70