1--TEST--
2mysqli_stmt_affected_rows()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11    require_once("connect.inc");
12
13    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
14        printf("Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
15            $host, $user, $db, $port, $socket);
16    }
17    $stmt = mysqli_stmt_init($link);
18
19    if (!mysqli_stmt_prepare($stmt, 'DROP TABLE IF EXISTS test') ||
20        !mysqli_stmt_execute($stmt)) {
21        printf("[003] Failed to drop old test table: [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
22    }
23
24    if (!mysqli_stmt_prepare($stmt, 'CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE = ' . $engine) ||
25        !mysqli_stmt_execute($stmt)) {
26        printf("[004] Failed to create test table: [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
27    }
28
29    if (0 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
30        printf("[005] Expecting int/0, got %s/'%s'\n", gettype($tmp), $tmp);
31
32    mysqli_stmt_close($stmt);
33    $stmt = mysqli_stmt_init($link);
34
35    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (1, 'a')") ||
36        !mysqli_stmt_execute($stmt))
37        printf("[006] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
38
39    mysqli_stmt_close($stmt);
40    $stmt = mysqli_stmt_init($link);
41
42    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (100, 'z')") ||
43        !mysqli_stmt_execute($stmt))
44        printf("[007] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
45
46    if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
47        printf("[008] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
48
49    mysqli_stmt_close($stmt);
50    $stmt = mysqli_stmt_init($link);
51
52    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (100, 'z')") ||
53        !mysqli_stmt_execute($stmt))
54        // NOTE: the error message varies with the MySQL Server version, dump only the error code!
55        printf("[009] [%d] (error message varies with the MySQL Server version, check the error code)\n", mysqli_stmt_errno($stmt));
56
57    /* an error occurred: affected rows should return -1 */
58    if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
59        printf("[010] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
60
61    mysqli_stmt_close($stmt);
62    $stmt = mysqli_stmt_init($link);
63
64    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (1, 'a') ON DUPLICATE KEY UPDATE id = 4") ||
65        !mysqli_stmt_execute($stmt))
66        printf("[011] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
67
68    if (2 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
69        printf("[012] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
70
71    mysqli_stmt_close($stmt);
72    $stmt = mysqli_stmt_init($link);
73
74    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (2, 'b'), (3, 'c')") ||
75        !mysqli_stmt_execute($stmt))
76        printf("[013] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
77
78    if (2 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
79        printf("[014] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
80
81    mysqli_stmt_close($stmt);
82    $stmt = mysqli_stmt_init($link);
83
84    if (!mysqli_stmt_prepare($stmt, "INSERT IGNORE INTO test(id, label) VALUES (1, 'a')") ||
85        !mysqli_stmt_execute($stmt))
86        printf("[015] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
87
88    if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
89        printf("[016] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
90
91    if (!($res = mysqli_query($link, "SELECT count(id) AS num FROM test")) ||
92        !($tmp = mysqli_fetch_assoc($res)))
93        printf("[017] [%d] %s\n", mysqli_error($link), mysqli_error($link));
94    $num = (int)$tmp['num'];
95    mysqli_free_result($res);
96
97    mysqli_stmt_close($stmt);
98    $stmt = mysqli_stmt_init($link);
99
100    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) SELECT id + 10, label FROM test") ||
101        !mysqli_stmt_execute($stmt))
102        printf("[018] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
103
104    if ($num !== ($tmp = mysqli_stmt_affected_rows($stmt)))
105        printf("[019] Expecting int/%d, got %s/%s\n", $num, gettype($tmp), $tmp);
106
107    mysqli_stmt_close($stmt);
108    $stmt = mysqli_stmt_init($link);
109
110    if (!mysqli_stmt_prepare($stmt, "REPLACE INTO test(id, label) values (4, 'd')") ||
111        !mysqli_stmt_execute($stmt))
112        printf("[020] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
113
114    if (2 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
115        printf("[021] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
116
117    mysqli_stmt_close($stmt);
118    $stmt = mysqli_stmt_init($link);
119
120    if (!mysqli_stmt_prepare($stmt, "REPLACE INTO test(id, label) values (5, 'e')") ||
121        !mysqli_stmt_execute($stmt))
122        printf("[022] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
123
124    if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
125        printf("[023] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
126
127    mysqli_stmt_close($stmt);
128    $stmt = mysqli_stmt_init($link);
129
130    if (!mysqli_stmt_prepare($stmt, "UPDATE test SET label = 'a' WHERE id = 2") ||
131        !mysqli_stmt_execute($stmt))
132        printf("[024] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
133
134    if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
135        printf("[025] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
136
137    if (!mysqli_stmt_prepare($stmt, "UPDATE test SET label = 'a' WHERE id = 2") ||
138        !mysqli_stmt_execute($stmt))
139        printf("[026] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
140
141    if (0 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
142        printf("[027] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
143
144    mysqli_stmt_close($stmt);
145    $stmt = mysqli_stmt_init($link);
146
147    if (!mysqli_stmt_prepare($stmt, "UPDATE test SET label = 'a' WHERE id = 100") ||
148        !mysqli_stmt_execute($stmt))
149        printf("[028] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
150
151    if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
152        printf("[029] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
153
154    if (!mysqli_stmt_prepare($stmt, 'SELECT label FROM test WHERE id = 100') ||
155        !mysqli_stmt_execute($stmt))
156        printf("[030] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
157
158    /* use it like num_rows */
159    /* PS are unbuffered, num_rows cannot determine the row count before all rows have been fetched and/or buffered */
160    if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
161        printf("[031] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);
162
163    if (0 !== ($tmp = mysqli_stmt_num_rows($stmt)))
164        printf("[032] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
165
166    if (!mysqli_stmt_store_result($stmt))
167        printf("[033] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
168
169    if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
170        printf("[034] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
171
172    if (1 !== ($tmp = mysqli_stmt_num_rows($stmt)))
173        printf("[035] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
174
175    mysqli_stmt_free_result($stmt);
176    mysqli_stmt_close($stmt);
177    $stmt = mysqli_stmt_init($link);
178
179    if (!mysqli_stmt_prepare($stmt, 'SELECT label FROM test WHERE 1 = 2') ||
180        !mysqli_stmt_execute($stmt))
181        printf("[036] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
182
183    /* use it like num_rows */
184    if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
185        printf("[037] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);
186
187    if (true !== ($tmp = mysqli_stmt_store_result($stmt)))
188        printf("[038] Expecting boolean/true, got %s\%s\n", gettype($tmp), $tmp);
189
190    if (0 !== ($tmp = mysqli_stmt_num_rows($stmt)))
191        printf("[039] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
192
193    if (0 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
194        printf("[040] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
195
196    /* try to use stmt_affected_rows like stmt_num_rows */
197    /* stmt_affected_rows is not really meant for SELECT! */
198    if (mysqli_stmt_prepare($stmt, 'SELECT unknown_column FROM this_table_does_not_exist') ||
199        mysqli_stmt_execute($stmt))
200        printf("[041] Expecting SELECT statement to fail on purpose\n");
201
202    if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
203        printf("[042] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);
204
205    if (true !== ($tmp = mysqli_stmt_store_result($stmt)))
206        printf("[043] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
207
208    if (0 !== ($tmp = mysqli_stmt_num_rows($stmt)))
209        printf("[044] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
210
211    if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
212        printf("[045] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);
213
214    mysqli_stmt_close($stmt);
215    $stmt = mysqli_stmt_init($link);
216
217    if (!mysqli_stmt_prepare($stmt, "DROP TABLE IF EXISTS test") ||
218        !mysqli_stmt_execute($stmt))
219        printf("[046] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
220
221    mysqli_stmt_close($stmt);
222
223    try {
224        mysqli_stmt_affected_rows($stmt);
225    } catch (Error $exception) {
226        echo $exception->getMessage() . "\n";
227    }
228
229    mysqli_close($link);
230
231    print "done!";
232?>
233--CLEAN--
234<?php
235    require_once("clean_table.inc");
236?>
237--EXPECTF--
238[009] [%d] (error message varies with the MySQL Server version, check the error code)
239mysqli_stmt object is already closed
240done!
241