1--TEST--
2EXPLAIN - metadata
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8?>
9--FILE--
10<?php
11    require_once 'table.inc';
12
13    if (!$res = mysqli_query($link, 'EXPLAIN SELECT t1.*, t2.* FROM test AS t1, test AS t2'))
14        printf("[001] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
15
16    $num_rows 	= 0;
17    $num_fields 	= 0;
18    $field_names	= array();
19    if (!$row = mysqli_fetch_assoc($res)) {
20        printf("[002] Expecting result but got no data [%d] %s\n",
21            mysqli_errno($link), mysqli_error($link));
22    } else {
23        $num_rows++;
24        $num_fields = count($row);
25        foreach ($row as $name => $value)
26            $field_names[$name] = gettype($value);
27    }
28
29    while ($row = mysqli_fetch_assoc($res))
30        $num_rows++;
31
32    if (($tmp = mysqli_num_rows($res)) !== $num_rows) {
33        printf("[003] Expecting int/%d got %s/%s\n",
34            $num_rows, gettype($tmp), $tmp);
35    }
36    if (($tmp = mysqli_field_count($link)) !== $num_fields) {
37        printf("[004] Expecting int/%d got %s/%s\n",
38            $num_fields, gettype($tmp), $tmp);
39    }
40    $fields = mysqli_fetch_fields($res);
41    if (($tmp = count($fields)) !== $num_fields) {
42        printf("[005] Expecting int/%d got %s/%s\n",
43            $num_fields, gettype($tmp), $tmp);
44    }
45
46    foreach ($fields as $k => $field) {
47        $field->max_length = 0;// change it or we will get diff error
48        if (isset($field_names[$field->name])) {
49            unset($field_names[$field->name]);
50        } else {
51            printf("[006] Unexpected field '%s', dumping info\n");
52            var_dump($field);
53        }
54    }
55    if (!empty($field_names)) {
56        printf("[007] Field descriptions missing for the following columns\n");
57        var_dump($field_names);
58    }
59
60    mysqli_free_result($res);
61
62    $stmt = mysqli_stmt_init($link);
63    /* Depending on your version, the MySQL server migit not support this */
64    if ($stmt->prepare('EXPLAIN SELECT t1.*, t2.* FROM test AS t1, test AS t2') && $stmt->execute()) {
65        if (!mysqli_stmt_store_result($stmt))
66            printf("[008] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
67
68        if (!$res_meta = mysqli_stmt_result_metadata($stmt))
69            printf("[009] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
70
71        if (($tmp = mysqli_stmt_num_rows($stmt)) !== $num_rows) {
72            printf("[010] Expecting int/%d got %s/%s\n",
73                $num_rows, gettype($tmp), $tmp);
74        }
75        if (($tmp = mysqli_stmt_field_count($stmt)) !== $num_fields) {
76            printf("[011] Expecting int/%d got %s/%s\n",
77                $num_fields, gettype($tmp), $tmp);
78        }
79        if (($tmp = mysqli_field_count($link)) !== $num_fields) {
80            printf("[013] Expecting int/%d got %s/%s\n",
81                $num_fields, gettype($tmp), $tmp);
82        }
83        if (($tmp = $res_meta->field_count) !== $num_fields) {
84            printf("[014] Expecting int/%d got %s/%s\n",
85                $num_fields, gettype($tmp), $tmp);
86        }
87        $fields_res_meta = mysqli_fetch_fields($res_meta);
88        if (($tmp = count($fields_res_meta)) !== $num_fields)
89            printf("[015] Expecting int/%d got %s/%s\n",
90                $num_fields, gettype($tmp), $tmp);
91
92        if ($fields_res_meta != $fields) {
93            printf("[016] Prepared Statement metadata differs from normal metadata, dumping\n");
94            var_dump($fields_res_meta);
95            var_dump($fields);
96        }
97
98        if (function_exists('mysqli_stmt_get_result') &&
99            $stmt->prepare('EXPLAIN SELECT t1.*, t2.* FROM test AS t1, test AS t2') &&
100            $stmt->execute()) {
101            if (!$res_stmt = mysqli_stmt_get_result($stmt)) {
102                printf("[017] Cannot fetch result from PS [%d] %s\n",
103                    mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
104            }
105            if (($tmp = mysqli_num_rows($res_stmt)) !== $num_rows) {
106                printf("[018] Expecting int/%d got %s/%s\n",
107                    $num_rows, gettype($tmp), $tmp);
108            }
109            if ((mysqli_stmt_num_rows($stmt)) !== 0) {
110                printf("[019] Expecting int/0 got %s/%s\n", gettype($tmp), $tmp);
111            }
112            if (($tmp = mysqli_stmt_field_count($stmt)) !== $num_fields) {
113                printf("[020] Expecting int/%d got %s/%s\n",
114                    $num_fields, gettype($tmp), $tmp);
115
116            }
117            if (($tmp = $res_stmt->field_count) !== $num_fields) {
118                printf("[021] Expecting int/%d got %s/%s\n",
119                    $num_fields, gettype($tmp), $tmp);
120            }
121
122            $fields_stmt = mysqli_fetch_fields($res_stmt);
123            if (($tmp = count($fields_stmt)) !== $num_fields) {
124                printf("[022] Expecting int/%d got %s/%s\n",
125                    $num_fields, gettype($tmp), $tmp);
126            }
127            reset($fields);
128            foreach ($fields_stmt as $fields_stmt_val) {
129                $fields_val = current($fields);
130                next($fields);
131                unset($fields_stmt_val->max_length);
132                unset($fields_val->max_length);
133                if ($fields_stmt_val != $fields_val) {
134                    printf("[023] PS mysqli_stmt_get_result() metadata seems wrong, dumping\n");
135                    var_dump($fields_stmt_val);
136                    var_dump($fields_val);
137                }
138            }
139/*
140            if ($fields_stmt != $fields) {
141                printf("[023] PS mysqli_stmt_get_result() metadata seems wrong, dumping\n");
142                var_dump($fields_stmt);
143                var_dump($fields);
144            }
145*/
146            mysqli_free_result($res_stmt);
147        }
148    }
149    mysqli_stmt_close($stmt);
150
151    mysqli_close($link);
152    print "done!";
153?>
154--CLEAN--
155<?php
156require_once 'clean_table.inc';
157?>
158--EXPECT--
159done!
160