1
+ --TEST--
2
+ mysqli_begin_transaction()
3
+ --SKIPIF--
4
+ <?php
5
+ require_once ('skipif.inc ' );
6
+ require_once ('skipifemb.inc ' );
7
+ require_once ('skipifconnectfailure.inc ' );
8
+
9
+ require_once ('connect.inc ' );
10
+ if (!$ link = my_mysqli_connect ($ host , $ user , $ passwd , $ db , $ port , $ socket ))
11
+ die (sprintf ("Cannot connect, [%d] %s " , mysqli_connect_errno (), mysqli_connect_error ()));
12
+
13
+ if (!have_innodb ($ link ))
14
+ die (sprintf ("Needs InnoDB support, [%d] %s " , $ link ->errno , $ link ->error ));
15
+ ?>
16
+ --FILE--
17
+ <?php
18
+ require_once ("connect.inc " );
19
+ /* {{{ proto bool mysqli_begin_transaction(object link, [int flags [, string name]]) */
20
+ $ tmp = NULL ;
21
+ $ link = NULL ;
22
+
23
+ if (!is_null ($ tmp = @mysqli_begin_transaction ()))
24
+ printf ("[001] Expecting NULL, got %s/%s \n" , gettype ($ tmp ), $ tmp );
25
+
26
+ if (!is_null ($ tmp = @mysqli_begin_transaction ($ link )))
27
+ printf ("[002] Expecting NULL, got %s/%s \n" , gettype ($ tmp ), $ tmp );
28
+
29
+ if (!is_null ($ tmp = @mysqli_begin_transaction ($ link , $ link )))
30
+ printf ("[003] Expecting NULL, got %s/%s \n" , gettype ($ tmp ), $ tmp );
31
+
32
+ if (!$ link = my_mysqli_connect ($ host , $ user , $ passwd , $ db , $ port , $ socket ))
33
+ printf ("[004] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s \n" ,
34
+ $ host , $ user , $ db , $ port , $ socket );
35
+
36
+ if (!is_null ($ tmp = @mysqli_begin_transaction ($ link , $ link )))
37
+ printf ("[005] Expecting NULL, got %s/%s \n" , gettype ($ tmp ), $ tmp );
38
+
39
+ if (!is_null ($ tmp = @mysqli_begin_transaction ($ link , 0 , $ link )))
40
+ printf ("[006] Expecting NULL, got %s/%s \n" , gettype ($ tmp ), $ tmp );
41
+
42
+ if (!is_null ($ tmp = @mysqli_begin_transaction ($ link , 0 , "mytrx " , $ link )))
43
+ printf ("[007] Expecting NULL, got %s/%s \n" , gettype ($ tmp ), $ tmp );
44
+
45
+ if (!mysqli_query ($ link , 'DROP TABLE IF EXISTS test ' ))
46
+ printf ("[008] [%d] %s \n" , mysqli_errno ($ link ), mysqli_error ($ link ));
47
+
48
+ if (!mysqli_query ($ link , 'CREATE TABLE test(id INT) ENGINE = InnoDB ' ))
49
+ printf ("[009] Cannot create test table, [%d] %s \n" , mysqli_errno ($ link ), mysqli_error ($ link ));
50
+
51
+ if (true !== ($ tmp = mysqli_autocommit ($ link , true )))
52
+ printf ("[010] Cannot turn on autocommit, expecting true, got %s/%s \n" , gettype ($ tmp ), $ tmp );
53
+
54
+ /* overrule autocommit */
55
+ if (true !== ($ tmp = mysqli_begin_transaction ($ link )))
56
+ printf ("[011] Got %s - [%d] %s \n" , var_dump ($ tmp , true ), mysqli_errno ($ link ), mysqli_error ($ link ));
57
+
58
+ if (!mysqli_query ($ link , 'INSERT INTO test(id) VALUES (1) ' ))
59
+ printf ("[012] [%d] %s \n" , mysqli_errno ($ link ), mysqli_error ($ link ));
60
+
61
+ $ tmp = mysqli_rollback ($ link );
62
+ if ($ tmp !== true )
63
+ printf ("[013] Expecting boolean/true, got %s/%s \n" , gettype ($ tmp ), $ tmp );
64
+
65
+ /* empty */
66
+ $ res = mysqli_query ($ link , "SELECT * FROM test " );
67
+ var_dump ($ res ->fetch_assoc ());
68
+
69
+ /* valid flags */
70
+ $ flags = array (
71
+ MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT ,
72
+ MYSQLI_TRANS_START_READ_WRITE ,
73
+ MYSQLI_TRANS_START_READ_ONLY ,
74
+ MYSQLI_TRANS_COR_AND_CHAIN ,
75
+ MYSQLI_TRANS_COR_AND_NO_CHAIN ,
76
+ MYSQLI_TRANS_COR_RELEASE ,
77
+ MYSQLI_TRANS_COR_NO_RELEASE );
78
+
79
+ /* just coverage */
80
+ foreach ($ flags as $ flag ) {
81
+ if (!mysqli_begin_transaction ($ link , $ flag , sprintf ("flag %d " , $ flag ))) {
82
+ printf ("[014] [%d] %s \n" , mysqli_errno ($ link ), mysqli_error ($ link ));
83
+ }
84
+ if (!mysqli_query ($ link , 'SELECT * FROM test ' ) ||
85
+ !mysqli_rollback ($ link )) {
86
+ printf ("[015] [%d] %s \n" , mysqli_errno ($ link ), mysqli_error ($ link ));
87
+ }
88
+ }
89
+
90
+ /* does it really set a flag? */
91
+ if (mysqli_get_server_version ($ link ) >= 50600 ) {
92
+ if (!mysqli_begin_transaction ($ link , MYSQLI_TRANS_START_READ_ONLY , sprintf ("flag %d " , $ flag ))) {
93
+ printf ("[016] [%d] %s \n" , mysqli_errno ($ link ), mysqli_error ($ link ));
94
+ }
95
+ if (!mysqli_query ($ link , "INSERT INTO test(id) VALUES (2) " ) ||
96
+ !mysqli_commit ($ link )) {
97
+ printf ("[017] [%d] %s \n" , mysqli_errno ($ link ), mysqli_error ($ link ));
98
+ } else {
99
+ $ res = mysqli_query ($ link , "SELECT id FROM test WHERE id = 2 " );
100
+ var_dump ($ res ->fetch_assoc ());
101
+ }
102
+ }
103
+
104
+ /* invalid flag */
105
+ do {
106
+ $ invalid_flag = mt_rand (0 , 10000 );
107
+ } while (isset (array_flip ($ flags )[$ invalid_flag ]));
108
+ /* we may or may not hit an invalid combination provoking a SQL error */
109
+ if (!mysqli_begin_transaction ($ link , $ invalid_flag , sprintf ("flag %d " , $ invalid_flag ))) {
110
+ printf ("[018] invalid_flag = %d [%d] %s \n" , $ invalid_flag , mysqli_errno ($ link ), mysqli_error ($ link ));
111
+ } else {
112
+ printf ("[018] invalid_flag = %d [%d] %s \n" , $ invalid_flag , mysqli_errno ($ link ), mysqli_error ($ link ));
113
+ }
114
+ if (!mysqli_begin_transaction ($ link , -1 )) {
115
+ printf ("[019] [%d] %s \n" , mysqli_errno ($ link ), mysqli_error ($ link ));
116
+ }
117
+
118
+ /* does it like stupid names? */
119
+ if (!$ link ->begin_transaction (MYSQLI_TRANS_START_READ_WRITE , "*/trick me? \n\0" ))
120
+ printf ("[020] [%d] %s \n" , mysqli_errno ($ link ), mysqli_error ($ link ));
121
+
122
+ /* does it like stupid names? */
123
+ if (!$ link ->begin_transaction (MYSQLI_TRANS_START_READ_WRITE , "az09 " ))
124
+ printf ("[021] [%d] %s \n" , mysqli_errno ($ link ), mysqli_error ($ link ));
125
+
126
+ print "done! " ;
127
+ ?>
128
+ --CLEAN--
129
+ <?php
130
+ require_once ("clean_table.inc " );
131
+ ?>
132
+ --EXPECTF--
133
+ NULL
134
+ [017] [1792] %s
135
+ [018] invalid_flag = %d [%d]%A
136
+
137
+ Warning: mysqli_begin_transaction(): Invalid value for parameter flags (-1) in %s on line %d
138
+ [019] [%d]%A
139
+ [020] [%d]%A
140
+ done!
0 commit comments