summaryrefslogtreecommitdiff
path: root/ext/mysqli/tests/mysqli_stmt_affected_rows.phpt
blob: 7fc32f7e61b37cab50c79030bd0652164db37349 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
--TEST--
mysqli_stmt_affected_rows()
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('skipifemb.inc');
require_once('skipifconnectfailure.inc');
?>
--FILE--
<?php
	require_once("connect.inc");

	$tmp    = NULL;
	$link   = NULL;

	if (!is_null($tmp = @mysqli_stmt_affected_rows()))
		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);

	if (!is_null($tmp = @mysqli_stmt_affected_rows($link)))
		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);

	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
		printf("Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
			$host, $user, $db, $port, $socket);
	}
	$stmt = mysqli_stmt_init($link);

	if (!mysqli_stmt_prepare($stmt, 'DROP TABLE IF EXISTS test') ||
		!mysqli_stmt_execute($stmt)) {
		printf("[003] Failed to drop old test table: [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
	}

	if (!mysqli_stmt_prepare($stmt, 'CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE = ' . $engine) ||
		!mysqli_stmt_execute($stmt)) {
		printf("[004] Failed to create test table: [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
	}

	if (0 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[005] Expecting int/0, got %s/'%s'\n", gettype($tmp), $tmp);

	mysqli_stmt_close($stmt);
	$stmt = mysqli_stmt_init($link);

	if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (1, 'a')") ||
		!mysqli_stmt_execute($stmt))
		printf("[006] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	mysqli_stmt_close($stmt);
	$stmt = mysqli_stmt_init($link);

	if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (100, 'z')") ||
		!mysqli_stmt_execute($stmt))
		printf("[007] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[008] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);

	mysqli_stmt_close($stmt);
	$stmt = mysqli_stmt_init($link);

	if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (100, 'z')") ||
		!mysqli_stmt_execute($stmt))
		// NOTE: the error message varies with the MySQL Server version, dump only the error code!
		printf("[009] [%d] (error message varies with the MySQL Server version, check the error code)\n", mysqli_stmt_errno($stmt));

	/* an error occurred: affected rows should return -1 */
	if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[010] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);

	mysqli_stmt_close($stmt);
	$stmt = mysqli_stmt_init($link);

	if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (1, 'a') ON DUPLICATE KEY UPDATE id = 4") ||
		!mysqli_stmt_execute($stmt))
		printf("[011] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	if (2 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[012] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);

	mysqli_stmt_close($stmt);
	$stmt = mysqli_stmt_init($link);

	if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (2, 'b'), (3, 'c')") ||
		!mysqli_stmt_execute($stmt))
		printf("[013] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	if (2 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[014] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);

	mysqli_stmt_close($stmt);
	$stmt = mysqli_stmt_init($link);

	if (!mysqli_stmt_prepare($stmt, "INSERT IGNORE INTO test(id, label) VALUES (1, 'a')") ||
		!mysqli_stmt_execute($stmt))
		printf("[015] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[016] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);

	if (!($res = mysqli_query($link, "SELECT count(id) AS num FROM test")) ||
		!($tmp = mysqli_fetch_assoc($res)))
		printf("[017] [%d] %s\n", mysqli_error($link), mysqli_error($link));
	$num = (int)$tmp['num'];
	mysqli_free_result($res);

	mysqli_stmt_close($stmt);
	$stmt = mysqli_stmt_init($link);

	if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) SELECT id + 10, label FROM test") ||
		!mysqli_stmt_execute($stmt))
		printf("[018] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	if ($num !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[019] Expecting int/%d, got %s/%s\n", $num, gettype($tmp), $tmp);

	mysqli_stmt_close($stmt);
	$stmt = mysqli_stmt_init($link);

	if (!mysqli_stmt_prepare($stmt, "REPLACE INTO test(id, label) values (4, 'd')") ||
		!mysqli_stmt_execute($stmt))
		printf("[020] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	if (2 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[021] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);

	mysqli_stmt_close($stmt);
	$stmt = mysqli_stmt_init($link);

	if (!mysqli_stmt_prepare($stmt, "REPLACE INTO test(id, label) values (5, 'e')") ||
		!mysqli_stmt_execute($stmt))
		printf("[022] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[023] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);

	mysqli_stmt_close($stmt);
	$stmt = mysqli_stmt_init($link);

	if (!mysqli_stmt_prepare($stmt, "UPDATE test SET label = 'a' WHERE id = 2") ||
		!mysqli_stmt_execute($stmt))
		printf("[024] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[025] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);

	if (!mysqli_stmt_prepare($stmt, "UPDATE test SET label = 'a' WHERE id = 2") ||
		!mysqli_stmt_execute($stmt))
		printf("[026] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	if (0 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[027] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);

	mysqli_stmt_close($stmt);
	$stmt = mysqli_stmt_init($link);

	if (!mysqli_stmt_prepare($stmt, "UPDATE test SET label = 'a' WHERE id = 100") ||
		!mysqli_stmt_execute($stmt))
		printf("[028] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[029] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);

	if (!mysqli_stmt_prepare($stmt, 'SELECT label FROM test WHERE id = 100') ||
		!mysqli_stmt_execute($stmt))
		printf("[030] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	/* use it like num_rows */
	/* PS are unbuffered, num_rows cannot determine the row count before all rows have been fetched and/or buffered */
	if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[031] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);

	if (0 !== ($tmp = mysqli_stmt_num_rows($stmt)))
		printf("[032] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);

	if (!mysqli_stmt_store_result($stmt))
		printf("[033] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[034] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);

	if (1 !== ($tmp = mysqli_stmt_num_rows($stmt)))
		printf("[035] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);

	mysqli_stmt_free_result($stmt);
	mysqli_stmt_close($stmt);
	$stmt = mysqli_stmt_init($link);

	if (!mysqli_stmt_prepare($stmt, 'SELECT label FROM test WHERE 1 = 2') ||
		!mysqli_stmt_execute($stmt))
		printf("[036] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	/* use it like num_rows */
	if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[037] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);

	if (true !== ($tmp = mysqli_stmt_store_result($stmt)))
		printf("[038] Expecting boolean/true, got %s\%s\n", gettype($tmp), $tmp);

	if (0 !== ($tmp = mysqli_stmt_num_rows($stmt)))
		printf("[039] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);

	if (0 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[040] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);

	/* try to use stmt_affected_rows like stmt_num_rows */
	/* stmt_affected_rows is not really meant for SELECT! */
	if (mysqli_stmt_prepare($stmt, 'SELECT unknown_column FROM this_table_does_not_exist') ||
		mysqli_stmt_execute($stmt))
		printf("[041] The invalid SELECT statement is issued on purpose\n");

	if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[042] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);

	if ($IS_MYSQLND) {
		if (false !== ($tmp = mysqli_stmt_store_result($stmt)))
			printf("[043] Expecting boolean/false, got %s\%s\n", gettype($tmp), $tmp);
	} else {
		if (true !== ($tmp = mysqli_stmt_store_result($stmt)))
			printf("[043] Libmysql does not care if the previous statement was bogus, expecting boolean/true, got %s\%s\n", gettype($tmp), $tmp);
	}

	if (0 !== ($tmp = mysqli_stmt_num_rows($stmt)))
		printf("[044] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);

	if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[045] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);

	mysqli_stmt_close($stmt);
	$stmt = mysqli_stmt_init($link);

	if (!mysqli_stmt_prepare($stmt, "DROP TABLE IF EXISTS test") ||
		!mysqli_stmt_execute($stmt))
		printf("[046] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));

	mysqli_stmt_close($stmt);

	if (!is_null($tmp = mysqli_stmt_affected_rows($stmt)))
		printf("[047] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);

	mysqli_close($link);

	print "done!";
?>
--CLEAN--
<?php
	require_once("clean_table.inc");
?>
--EXPECTF--
[009] [%d] (error message varies with the MySQL Server version, check the error code)

Warning: mysqli_stmt_affected_rows(): Couldn't fetch mysqli_stmt in %s on line %d
done!