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