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