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 (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 $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", $field->name);
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 ($stmt->prepare('EXPLAIN SELECT t1.*, t2.* FROM test AS t1, test AS t2') &&
99            $stmt->execute()) {
100            if (!$res_stmt = mysqli_stmt_get_result($stmt)) {
101                printf("[017] Cannot fetch result from PS [%d] %s\n",
102                    mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
103            }
104            if (($tmp = mysqli_num_rows($res_stmt)) !== $num_rows) {
105                printf("[018] Expecting int/%d got %s/%s\n",
106                    $num_rows, gettype($tmp), $tmp);
107            }
108            if ((mysqli_stmt_num_rows($stmt)) !== 0) {
109                printf("[019] Expecting int/0 got %s/%s\n", gettype($tmp), $tmp);
110            }
111            if (($tmp = mysqli_stmt_field_count($stmt)) !== $num_fields) {
112                printf("[020] Expecting int/%d got %s/%s\n",
113                    $num_fields, gettype($tmp), $tmp);
114
115            }
116            if (($tmp = $res_stmt->field_count) !== $num_fields) {
117                printf("[021] Expecting int/%d got %s/%s\n",
118                    $num_fields, gettype($tmp), $tmp);
119            }
120
121            $fields_stmt = mysqli_fetch_fields($res_stmt);
122            if (($tmp = count($fields_stmt)) !== $num_fields) {
123                printf("[022] Expecting int/%d got %s/%s\n",
124                    $num_fields, gettype($tmp), $tmp);
125            }
126            reset($fields);
127            foreach ($fields_stmt as $fields_stmt_val) {
128                $fields_val = current($fields);
129                next($fields);
130                unset($fields_stmt_val->max_length);
131                unset($fields_val->max_length);
132                if ($fields_stmt_val != $fields_val) {
133                    printf("[023] PS mysqli_stmt_get_result() metadata seems wrong, dumping\n");
134                    var_dump($fields_stmt_val);
135                    var_dump($fields_val);
136                }
137            }
138/*
139            if ($fields_stmt != $fields) {
140                printf("[023] PS mysqli_stmt_get_result() metadata seems wrong, dumping\n");
141                var_dump($fields_stmt);
142                var_dump($fields);
143            }
144*/
145            mysqli_free_result($res_stmt);
146        }
147    }
148    mysqli_stmt_close($stmt);
149
150    mysqli_close($link);
151    print "done!";
152?>
153--CLEAN--
154<?php
155require_once 'clean_table.inc';
156?>
157--EXPECT--
158done!
159