1--TEST--
2mysqli_stmt_get_result() - meta data
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8?>
9--FILE--
10<?php
11    require 'table.inc';
12
13    if (!$stmt = mysqli_stmt_init($link))
14        printf("[001] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
15
16    if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id ASC LIMIT 3"))
17        printf("[002] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
18
19    if (!mysqli_stmt_execute($stmt))
20        printf("[003] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
21
22    if (!is_object($res = mysqli_stmt_get_result($stmt)) || 'mysqli_result' != get_class($res)) {
23        printf("[004] Expecting object/mysqli_result got %s/%s, [%d] %s\n",
24            gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
25    }
26
27    if (!is_object($res_meta = mysqli_stmt_result_metadata($stmt)) ||
28        'mysqli_result' != get_class($res_meta)) {
29        printf("[005] Expecting object/mysqli_result got %s/%s, [%d] %s\n",
30            gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
31    }
32
33    var_dump(mysqli_fetch_assoc($res));
34    var_dump(mysqli_fetch_assoc($res_meta));
35
36    mysqli_free_result($res);
37    mysqli_free_result($res_meta);
38    mysqli_stmt_close($stmt);
39
40    // !mysqli_stmt_prepare($stmt, "SELECT id, label, id + 1 as _id,  concat(label, '_') _label FROM test as _test ORDER BY id ASC LIMIT 3") ||
41    if (!($stmt = mysqli_stmt_init($link)) ||
42        !mysqli_stmt_prepare($stmt, "SELECT id , label, id + 1 AS _id, label AS _label, null AS _null, CONCAT(label, '_') _label_concat  FROM test _test ORDER BY id ASC LIMIT 3") ||
43        !mysqli_stmt_execute($stmt))
44        printf("[006] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
45
46    if (!is_object($res = mysqli_stmt_get_result($stmt)) || 'mysqli_result' != get_class($res)) {
47        printf("[007] Expecting object/mysqli_result got %s/%s, [%d] %s\n",
48            gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
49    }
50
51    if (!is_object($res_meta = mysqli_stmt_result_metadata($stmt)) ||
52        'mysqli_result' != get_class($res_meta)) {
53        printf("[008] Expecting object/mysqli_result got %s/%s, [%d] %s\n",
54            gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
55    }
56
57    if (($tmp1 = mysqli_num_fields($res)) !== ($tmp2 = mysqli_num_fields($res_meta))) {
58        printf("[009] %s/%s !== %s/%s\n", gettype($tmp1), $tmp1, gettype($tmp2), $tmp2);
59    }
60
61    /*
62    if (($tmp1 = mysqli_field_count($link)) !== ($tmp2 = $res->field_count()))
63        printf("[010] %s/%s !== %s/%s\n", gettype($tmp1), $tmp1, gettype($tmp2), $tmp2);
64
65    if (($tmp1 = $res_meta->field_count()) !== $tmp2)
66        printf("[011] %s/%s !== %s/%s\n", gettype($tmp1), $tmp1, gettype($tmp2), $tmp2);
67    */
68
69    if (($tmp1 = mysqli_field_tell($res)) !== ($tmp2 = $res_meta->current_field))
70        printf("[012] %s/%s !== %s/%s\n", gettype($tmp1), $tmp1, gettype($tmp2), $tmp2);
71
72    if (0 !== $tmp1)
73        printf("[013] Expecting int/0 got %s/%s\n", gettype($tmp1), $tmp1);
74
75    $fields = array();
76    while ($info = $res->fetch_field())
77        $fields['res'][] = $info;
78    var_dump($info);
79    while ($info = $res_meta->fetch_field())
80        $fields['meta'][] = $info;
81    var_dump($info);
82    $fields['all_res'] = $res->fetch_fields();
83    $fields['all_meta'] = $res_meta->fetch_fields();
84
85    if (count($fields['res']) != count($fields['meta'])) {
86        printf("[014] stmt_get_result indicates %d fields, stmt_result_metadata indicates %d fields\n",
87            count($fields['res']),
88            count($fields['meta']));
89    }
90
91    foreach ($fields['res'] as $k => $info) {
92        printf("%s\n", $info->name);
93        if ($info->name !== $fields['meta'][$k]->name)
94            printf("[015 - %d] Expecting name %s/%s got %s/%s\n",
95                $k, gettype($info->name), $info->name, gettype($fields['meta'][$k]->name), $fields['meta'][$k]->name);
96
97        if ($info->orgname !== $fields['meta'][$k]->orgname)
98            printf("[016 - %d] Expecting orgname %s/%s got %s/%s\n",
99                $k, gettype($info->orgname), $info->orgname, gettype($fields['meta'][$k]->orgname), $fields['meta'][$k]->orgname);
100
101        if ($info->table !== $fields['meta'][$k]->table)
102            printf("[017 - %d] Expecting table %s/%s got %s/%s\n",
103                $k, gettype($info->table), $info->table, gettype($fields['meta'][$k]->table), $fields['meta'][$k]->table);
104
105        if ($info->orgtable !== $fields['meta'][$k]->orgtable)
106            printf("[018 - %d] Expecting orgtable %s/%s got %s/%s\n",
107                $k, gettype($info->orgtable), $info->orgtable, gettype($fields['meta'][$k]->orgtable), $fields['meta'][$k]->orgtable);
108
109        if ($info->def !== $fields['meta'][$k]->def)
110            printf("[019 - %d] Expecting def %s/%s got %s/%s\n",
111                $k, gettype($info->def), $info->def, gettype($fields['meta'][$k]->def), $fields['meta'][$k]->def);
112/*
113        if ($info->max_length !== $fields['meta'][$k]->max_length)
114            printf("[020 - %d] Expecting max_length %s/%s got %s/%s\n",
115                $k, gettype($info->max_length), $info->max_length, gettype($fields['meta'][$k]->max_length), $fields['meta'][$k]->max_length);
116*/
117        if ($info->length !== $fields['meta'][$k]->length)
118            printf("[021 - %d] Expecting length %s/%s got %s/%s\n",
119                $k, gettype($info->length), $info->length, gettype($fields['meta'][$k]->length), $fields['meta'][$k]->length);
120
121        if ($info->charsetnr !== $fields['meta'][$k]->charsetnr)
122            printf("[022 - %d] Expecting charsetnr %s/%s got %s/%s\n",
123                $k, gettype($info->charsetnr), $info->charsetnr, gettype($fields['meta'][$k]->charsetnr), $fields['meta'][$k]->charsetnr);
124
125        if ($info->flags !== $fields['meta'][$k]->flags)
126            printf("[023 - %d] Expecting flags %s/%s got %s/%s\n",
127                $k, gettype($info->flags), $info->flags, gettype($fields['meta'][$k]->flags), $fields['meta'][$k]->flags);
128
129        if ($info->type !== $fields['meta'][$k]->type)
130            printf("[024 - %d] Expecting type %s/%s got %s/%s\n",
131                $k, gettype($info->type), $info->type, gettype($fields['meta'][$k]->type), $fields['meta'][$k]->type);
132
133        if ($info->decimals !== $fields['meta'][$k]->decimals)
134            printf("[025 - %d] Expecting decimals %s/%s got %s/%s\n",
135                $k, getdecimals($info->decimals), $info->decimals, getdecimals($fields['meta'][$k]->decimals), $fields['meta'][$k]->decimals);
136
137        /* Make them equal for the check */
138        $tmp = $fields['all_res'][$k]->max_length;
139        $fields['all_res'][$k]->max_length = $fields['all_meta'][$k]->max_length;
140
141        if ($fields['all_res'][$k] != $fields['all_meta'][$k]) {
142            printf("[026 - %d] fetch_fields() seems to have returned different data, dumping\n", $k);
143            var_dump($fields['all_res'][$k]);
144            var_dump($fields['all_meta'][$k]);
145        }
146        $fields['all_res'][$k]->max_length = $tmp;
147    }
148
149    $num = count($fields['res']);
150    for ($i = 0; $i < 100; $i++) {
151        $pos = mt_rand(-1, $num + 1);
152        if ($pos >= 0 && $pos < $num) {
153            if (true !== mysqli_field_seek($res, $pos))
154                printf("[027] field_seek(res) failed\n");
155            if (true !== $res_meta->field_seek($pos))
156                printf("[028] field_seek(res__meta) failed\n");
157
158            $tmp1 = $res->fetch_field();
159            $tmp2 = mysqli_fetch_field($res_meta);
160            $tmp2->max_length = $tmp1->max_length;
161            if ($tmp1 != $tmp2) {
162                printf("[029] Field info differs, dumping data\n");
163                var_dump($tmp1);
164                var_dump($tmp2);
165            }
166
167            if ($tmp1 != $fields['res'][$pos]) {
168                printf("[030] Field info differs, dumping data\n");
169                var_dump($tmp1);
170                var_dump($fields['res'][$pos]);
171            }
172
173            $pos++;
174            if ($pos !== ($tmp = mysqli_field_tell($res)))
175                printf("[031] Expecting %s/%s got %s/%s\n",
176                    gettype($pos), $pos, gettype($tmp), $tmp);
177
178            if ($pos !== ($tmp = mysqli_field_tell($res_meta)))
179                printf("[032] Expecting %s/%s got %s/%s\n",
180                    gettype($pos), $pos, gettype($tmp), $tmp);
181        } else {
182            try {
183                $tmp = @mysqli_field_seek($res, $pos);
184                if ($pos >= $num && $tmp !== false) {
185                    printf("[033] field_seek(%d) did not fail\n", $pos);
186                }
187            } catch (ValueError $e) { /* Suppress output because pos is RANDOM */}
188
189            try {
190                $tmp = @mysqli_field_seek($res_meta, $pos);
191                if ($pos >= $num && $tmp !== false) {
192                    printf("[034] field_seek(%d) did not fail\n", $pos);
193                }
194            } catch (ValueError $e) { /* Suppress output because pos is RANDOM */}
195        }
196    }
197
198    $res->free_result();
199    mysqli_free_result($res_meta);
200
201    try {
202        mysqli_fetch_field($res);
203    } catch (Error $exception) {
204        echo $exception->getMessage() . "\n";
205    }
206
207    mysqli_stmt_close($stmt);
208
209    try {
210        mysqli_fetch_field($res);
211    } catch (Error $exception) {
212        echo $exception->getMessage() . "\n";
213    }
214
215    mysqli_close($link);
216
217    try {
218        mysqli_fetch_field($res);
219    } catch (Error $exception) {
220        echo $exception->getMessage() . "\n";
221    }
222
223    print "done!";
224?>
225--CLEAN--
226<?php
227    require_once 'clean_table.inc';
228?>
229--EXPECT--
230array(2) {
231  ["id"]=>
232  int(1)
233  ["label"]=>
234  string(1) "a"
235}
236NULL
237bool(false)
238bool(false)
239id
240label
241_id
242_label
243_null
244_label_concat
245mysqli_result object is already closed
246mysqli_result object is already closed
247mysqli_result object is already closed
248done!
249