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