1--TEST--
2mysqli_stmt_get_result - data types
3--SKIPIF--
4<?php
5    require_once('skipif.inc');
6    require_once('skipifconnectfailure.inc');
7
8    if (!function_exists('mysqli_stmt_get_result'))
9        die("skip mysqli_stmt_get_result() not available");
10?>
11--FILE--
12<?php
13    require('connect.inc');
14    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
15        printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
16
17    function func_mysqli_stmt_get_result($link, $engine, $bind_type, $sql_type, $bind_value, $offset, $type_hint = null) {
18
19        if (!mysqli_query($link, "DROP TABLE IF EXISTS test")) {
20            printf("[%04d] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
21            return false;
22        }
23
24        if (!mysqli_query($link, sprintf("CREATE TABLE test(id INT, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine))) {
25            // don't bail - column type might not be supported by the server, ignore this
26            return false;
27        }
28
29        if (!$stmt = mysqli_stmt_init($link)) {
30            printf("[%04d] [%d] %s\n", $offset + 1, mysqli_errno($link), mysqli_error($link));
31            return false;
32        }
33
34        if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (?, ?)")) {
35            printf("[%04d] [%d] %s\n", $offset + 2, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
36            return false;
37        }
38
39        $id = null;
40        if (!mysqli_stmt_bind_param($stmt, "i" . $bind_type, $id, $bind_value)) {
41            printf("[%04d] [%d] %s\n", $offset + 3, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
42            mysqli_stmt_close($stmt);
43            return false;
44        }
45
46        for ($id = 1; $id < 4; $id++) {
47            if (!mysqli_stmt_execute($stmt)) {
48                printf("[%04d] [%d] %s\n", $offset + 3 + $id, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
49                mysqli_stmt_close($stmt);
50                return false;
51            }
52        }
53        mysqli_stmt_close($stmt);
54
55        $stmt = mysqli_stmt_init($link);
56
57        if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test")) {
58            printf("[%04d] [%d] %s\n", $offset + 7, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
59            mysqli_stmt_close($stmt);
60            return false;
61        }
62
63        if (!mysqli_stmt_execute($stmt)) {
64            printf("[%04d] [%d] %s\n", $offset + 8, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
65            mysqli_stmt_close($stmt);
66            return false;
67        }
68
69        $result = mysqli_stmt_result_metadata($stmt);
70
71        if (!$res = mysqli_stmt_get_result($stmt)) {
72            printf("[%04d] [%d] %s\n", $offset + 9, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
73            mysqli_stmt_close($stmt);
74            return false;
75        }
76        $num = 0;
77        $fields = mysqli_fetch_fields($result);
78
79        while ($row = mysqli_fetch_assoc($res)) {
80            $bind_res = &$row['label'];
81            if (!gettype($bind_res) == 'unicode') {
82                if ($bind_res !== $bind_value && (!$type_hint || ($type_hint !== gettype($bind_res)))) {
83                    printf("[%04d] [%d] Expecting %s/'%s' [type hint = %s], got %s/'%s'\n",
84                        $offset + 10, $num,
85                        gettype($bind_value), $bind_value, $type_hint,
86                        gettype($bind_res), $bind_res);
87                        mysqli_free_result($res);
88                        mysqli_stmt_close($stmt);
89                        return false;
90                }
91            }
92            $num++;
93        }
94
95        if ($num != 3) {
96            printf("[%04d] [%d] %s, expecting 3 results, got only %d results\n",
97                $offset + 11, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), $num);
98            mysqli_free_result($res);
99            mysqli_stmt_close($stmt);
100            return false;
101        }
102
103        mysqli_free_result($res);
104        mysqli_stmt_close($stmt);
105        return true;
106    }
107
108
109    function func_mysqli_stmt_bind_make_string($len) {
110
111        $ret = '';
112        for ($i = 0; $i < $len; $i++)
113            $ret .= chr(mt_rand(65, 90));
114
115        return $ret;
116    }
117
118    func_mysqli_stmt_get_result($link, $engine, "i", "TINYINT", -11, 20);
119    func_mysqli_stmt_get_result($link, $engine, "i", "TINYINT", NULL, 40);
120    func_mysqli_stmt_get_result($link, $engine, "i", "TINYINT UNSIGNED", 1, 60);
121    func_mysqli_stmt_get_result($link, $engine, "i", "TINYINT UNSIGNED", NULL, 80);
122
123    func_mysqli_stmt_get_result($link, $engine, "i", "BOOL", 1, 100);
124    func_mysqli_stmt_get_result($link, $engine, "i", "BOOL", NULL, 120);
125    func_mysqli_stmt_get_result($link, $engine, "i", "BOOLEAN", 0, 140);
126    func_mysqli_stmt_get_result($link, $engine, "i", "BOOLEAN", NULL, 160);
127
128    func_mysqli_stmt_get_result($link, $engine, "i", "SMALLINT", -32768, 180);
129    func_mysqli_stmt_get_result($link, $engine, "i", "SMALLINT", 32767, 200);
130    func_mysqli_stmt_get_result($link, $engine, "i", "SMALLINT", NULL, 220);
131    func_mysqli_stmt_get_result($link, $engine, "i", "SMALLINT UNSIGNED", 65535, 240);
132    func_mysqli_stmt_get_result($link, $engine, "i", "SMALLINT UNSIGNED", NULL, 260);
133
134    func_mysqli_stmt_get_result($link, $engine, "d", "MEDIUMINT", -8388608, 280, "integer");
135    func_mysqli_stmt_get_result($link, $engine, "d", "MEDIUMINT", 8388607, 300, "integer");
136    func_mysqli_stmt_get_result($link, $engine, "d", "MEDIUMINT", NULL, 320);
137    func_mysqli_stmt_get_result($link, $engine, "d", "MEDIUMINT UNSIGNED", 16777215, 340, "integer");
138    func_mysqli_stmt_get_result($link, $engine, "d", "MEDIUMINT UNSIGNED", NULL, 360);
139
140    func_mysqli_stmt_get_result($link, $engine, "i", "INTEGER", (defined("PHP_INT_MAX")) ? max(-1 * PHP_INT_MAX + 1, -2147483648) : 1, 380);
141    func_mysqli_stmt_get_result($link, $engine, "i", "INTEGER", -2147483647, 400, "integer");
142    func_mysqli_stmt_get_result($link, $engine, "i", "INTEGER", (defined("PHP_INT_MAX")) ? min(2147483647, PHP_INT_MAX) : 1, 420);
143    func_mysqli_stmt_get_result($link, $engine, "i", "INTEGER", NULL, 440);
144    func_mysqli_stmt_get_result($link, $engine, "i", "INTEGER UNSIGNED", (defined("PHP_INT_MAX")) ? min(4294967295, 2147483647) : 1, 460);
145    func_mysqli_stmt_get_result($link, $engine, "i", "INTEGER UNSIGNED", 4294967295, 480, (defined("PHP_INT_MAX") && (4294967295 > PHP_INT_MAX)) ? "string" : null);
146    func_mysqli_stmt_get_result($link, $engine, "i", "INTEGER UNSIGNED", NULL, 500);
147
148    /* test is broken too: we bind "integer" but value is a float
149    func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT", -9223372036854775808, 520);
150    func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT UNSIGNED", 18446744073709551615, 560);
151    */
152    func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT", NULL, 540);
153    func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT UNSIGNED", NULL, 580);
154    func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT", -1, 1780);
155    func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT UNSIGNED", 1, 1800);
156    func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT", -1 * PHP_INT_MAX + 1, 1820);
157    func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT UNSIGNED", PHP_INT_MAX, 1840);
158    func_mysqli_stmt_get_result($link, $engine, "s", "BIGINT UNSIGNED", "18446744073709551615", 1860);
159    func_mysqli_stmt_get_result($link, $engine, "s", "BIGINT", "-9223372036854775808", 1880);
160
161    func_mysqli_stmt_get_result($link, $engine, "d", "FLOAT", -9223372036854775808 - 1.1, 600);
162    func_mysqli_stmt_get_result($link, $engine, "d", "FLOAT", NULL, 620);
163    func_mysqli_stmt_get_result($link, $engine, "d", "FLOAT UNSIGNED", 18446744073709551615 + 1.1, 640);
164    func_mysqli_stmt_get_result($link, $engine, "d", "FLOAT UNSIGNED ", NULL, 660);
165
166    // Yes, we need the temporary variable. The PHP casting will fouls us otherwise.
167    $tmp = strval('-99999999.99');
168    func_mysqli_stmt_get_result($link, $engine, "d", "DOUBLE(10,2)", $tmp, 680, "string");
169    func_mysqli_stmt_get_result($link, $engine, "d", "DOUBLE(10,2)", NULL, 700);
170    $tmp = strval('99999999.99');
171    func_mysqli_stmt_get_result($link, $engine, "d", "DOUBLE(10,2) UNSIGNED", $tmp , 720, "string");
172    func_mysqli_stmt_get_result($link, $engine, "d", "DOUBLE(10,2) UNSIGNED", NULL, 740);
173    $tmp = strval('-99999999.99');
174    func_mysqli_stmt_get_result($link, $engine, "d", "DECIMAL(10,2)", $tmp, 760, "string");
175    func_mysqli_stmt_get_result($link, $engine, "d", "DECIMAL(10,2)", NULL, 780);
176    $tmp = strval('99999999.99');
177    func_mysqli_stmt_get_result($link, $engine, "d", "DECIMAL(10,2)", $tmp, 800, "string");
178    func_mysqli_stmt_get_result($link, $engine, "d", "DECIMAL(10,2)", NULL, 820);
179
180    // don't care about date() strict TZ warnings...
181    func_mysqli_stmt_get_result($link, $engine, "s", "DATE", @date('Y-m-d'), 840);
182    func_mysqli_stmt_get_result($link, $engine, "s", "DATE NOT NULL", @date('Y-m-d'), 860);
183    func_mysqli_stmt_get_result($link, $engine, "s", "DATE", NULL, 880);
184
185    func_mysqli_stmt_get_result($link, $engine, "s", "DATETIME", @date('Y-m-d H:i:s'), 900);
186    func_mysqli_stmt_get_result($link, $engine, "s", "DATETIME NOT NULL", @date('Y-m-d H:i:s'), 920);
187    func_mysqli_stmt_get_result($link, $engine, "s", "DATETIME", NULL, 940);
188
189    func_mysqli_stmt_get_result($link, $engine, "s", "TIMESTAMP", @date('Y-m-d H:i:s'), 960);
190
191    func_mysqli_stmt_get_result($link, $engine, "s", "TIME", @date('H:i:s'), 980);
192    func_mysqli_stmt_get_result($link, $engine, "s", "TIME NOT NULL", @date('H:i:s'), 1000);
193    func_mysqli_stmt_get_result($link, $engine, "s", "TIME", NULL, 1020);
194
195    $tmp = intval(@date('Y'));
196    func_mysqli_stmt_get_result($link, $engine, "s", "YEAR", $tmp, 1040, "integer");
197    func_mysqli_stmt_get_result($link, $engine, "s", "YEAR NOT NULL", $tmp, 1060, "integer");
198    func_mysqli_stmt_get_result($link, $engine, "s", "YEAR", NULL, 1080);
199
200    $string255 = func_mysqli_stmt_bind_make_string(255);
201    func_mysqli_stmt_get_result($link, $engine, "s", "CHAR(1)", "a", 1110, 'string');
202    func_mysqli_stmt_get_result($link, $engine, "s", "CHAR(255)", $string255, 1120, 'string');
203    func_mysqli_stmt_get_result($link, $engine, "s", "CHAR(1) NOT NULL", "a", 1140, 'string');
204    func_mysqli_stmt_get_result($link, $engine, "s", "CHAR(1)", NULL, 1160);
205
206    $string65k = func_mysqli_stmt_bind_make_string(65535);
207    func_mysqli_stmt_get_result($link, $engine, "s", "VARCHAR(1)", "a", 1180, 'string');
208    func_mysqli_stmt_get_result($link, $engine, "s", "VARCHAR(255)", $string255, 1200, 'string');
209    func_mysqli_stmt_get_result($link, $engine, "s", "VARCHAR(65635)", $string65k, 1220, 'string');
210    func_mysqli_stmt_get_result($link, $engine, "s", "VARCHAR(1) NOT NULL", "a", 1240, 'string');
211    func_mysqli_stmt_get_result($link, $engine, "s", "VARCHAR(1)", NULL, 1260);
212
213    func_mysqli_stmt_get_result($link, $engine, "s", "BINARY(1)", "a", 1280);
214    func_mysqli_stmt_get_result($link, $engine, "s", "BINARY(1)", chr(0), 1300);
215    func_mysqli_stmt_get_result($link, $engine, "s", "BINARY(1) NOT NULL", "b", 1320);
216    func_mysqli_stmt_get_result($link, $engine, "s", "BINARY(1)", NULL, 1340);
217
218    func_mysqli_stmt_get_result($link, $engine, "s", "VARBINARY(1)", "a", 1360);
219    func_mysqli_stmt_get_result($link, $engine, "s", "VARBINARY(1)", chr(0), 1380);
220    func_mysqli_stmt_get_result($link, $engine, "s", "VARBINARY(1) NOT NULL", "b", 1400);
221    func_mysqli_stmt_get_result($link, $engine, "s", "VARBINARY(1)", NULL, 1420);
222
223    func_mysqli_stmt_get_result($link, $engine, "s", "TINYBLOB", "a", 1440);
224    func_mysqli_stmt_get_result($link, $engine, "s", "TINYBLOB", chr(0), 1460);
225    func_mysqli_stmt_get_result($link, $engine, "s", "TINYBLOB NOT NULL", "b", 1480);
226    func_mysqli_stmt_get_result($link, $engine, "s", "TINYBLOB", NULL, 1500);
227
228    func_mysqli_stmt_get_result($link, $engine, "s", "TINYTEXT", "a", 1520, 'string');
229    func_mysqli_stmt_get_result($link, $engine, "s", "TINYTEXT NOT NULL", "a", 1540, 'string');
230    func_mysqli_stmt_get_result($link, $engine, "s", "TINYTEXT", NULL, 1560, 'string');
231
232    // Note: you cannot insert any blob values this way. But you can check the API at least partly this way
233    // Extra BLOB tests are in mysqli_stmt_send_long()
234    func_mysqli_stmt_get_result($link, $engine, "b", "BLOB", "", 1580);
235    func_mysqli_stmt_get_result($link, $engine, "b", "TEXT", "", 1600, 'string');
236    func_mysqli_stmt_get_result($link, $engine, "b", "MEDIUMBLOB", "", 1620);
237    func_mysqli_stmt_get_result($link, $engine, "b", "MEDIUMTEXT", "", 1640, 'string');
238
239    /* Is this one related? http://bugs.php.net/bug.php?id=35759 */
240    func_mysqli_stmt_get_result($link, $engine, "b", "LONGBLOB", "", 1660);
241    func_mysqli_stmt_get_result($link, $engine, "b", "LONGTEXT", "", 1680, 'string');
242
243    func_mysqli_stmt_get_result($link, $engine, "s", "ENUM('a', 'b')", "a", 1700, 'string');
244    func_mysqli_stmt_get_result($link, $engine, "s", "ENUM('a', 'b')", NULL, 1720, 'string');
245    func_mysqli_stmt_get_result($link, $engine, "s", "SET('a', 'b')", "a", 1740, 'string');
246    func_mysqli_stmt_get_result($link, $engine, "s", "SET('a', 'b')", NULL, 1760, 'string');
247
248    mysqli_close($link);
249    print "done!";
250?>
251--CLEAN--
252<?php
253    require_once("clean_table.inc");
254?>
255--EXPECT--
256done!
257