1--TEST--
2mysqli_stmt_get_result()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8if (!function_exists('mysqli_stmt_get_result'))
9    die('skip mysqli_stmt_get_result not available');
10?>
11--FILE--
12<?php
13    /*
14    NOTE: no datatype tests here! This is done by
15    mysqli_stmt_bind_result.phpt already. Restrict
16    this test case to the basics.
17    */
18    require_once("connect.inc");
19
20    $tmp	= NULL;
21    $link   = NULL;
22
23    if (!is_null($tmp = @mysqli_stmt_get_result()))
24        printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
25
26    if (!is_null($tmp = @mysqli_stmt_get_result($link)))
27        printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
28
29    require('table.inc');
30
31    if (!$stmt = mysqli_stmt_init($link))
32        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
33
34    if (!is_null($tmp = @mysqli_stmt_get_result($stmt, "foo")))
35        printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
36
37    if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id ASC LIMIT 1"))
38        printf("[005] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
39
40        if (!mysqli_stmt_execute($stmt))
41            printf("[006] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
42
43        if (!is_object($res = mysqli_stmt_get_result($stmt)) || 'mysqli_result' != get_class($res)) {
44            printf("[007] Expecting object/mysqli_result got %s/%s, [%d] %s\n",
45                gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
46        }
47        while ($row = mysqli_fetch_assoc($res))
48            var_dump($row);
49        var_dump(mysqli_fetch_assoc($res));
50        mysqli_free_result($res);
51
52        if (false !== ($res = mysqli_stmt_get_result($stmt))) {
53            printf("[008] boolean/false got %s/%s, [%d] %s\n",
54                gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
55        }
56
57        mysqli_stmt_execute($stmt);
58        if (!is_object($res = mysqli_stmt_get_result($stmt)) || 'mysqli_result' != get_class($res)) {
59            printf("[009] Expecting object/mysqli_result got %s/%s, [%d] %s\n",
60                gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
61        }
62        while ($row = mysqli_fetch_assoc($res))
63            var_dump($row);
64        var_dump(mysqli_fetch_assoc($res));
65        mysqli_free_result($res);
66
67    mysqli_stmt_close($stmt);
68
69    if (!($stmt = mysqli_stmt_init($link)) ||
70        !mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id ASC LIMIT 2") ||
71        !mysqli_stmt_execute($stmt))
72        printf("[010] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
73
74    $id = $label = null;
75    if (!mysqli_stmt_bind_result($stmt, $id, $label))
76        printf("[011] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
77
78    if (!mysqli_stmt_fetch($stmt))
79        printf("[012] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
80
81    if (false !== ($res = mysqli_stmt_get_result($stmt))) {
82        printf("[013] boolean/false got %s/%s, [%d] %s\n",
83            gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
84    }
85
86    mysqli_stmt_close($stmt);
87
88    if (!($stmt = mysqli_stmt_init($link)) ||
89        !mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id ASC LIMIT 2") ||
90        !mysqli_stmt_execute($stmt))
91        printf("[014] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
92
93    if (!is_object($res = mysqli_stmt_get_result($stmt)) || 'mysqli_result' != get_class($res)) {
94        printf("[015] Expecting object/mysqli_result got %s/%s, [%d] %s\n",
95            gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
96    }
97
98    $id = $label = null;
99    if (!mysqli_stmt_bind_result($stmt, $id, $label))
100        printf("[016] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
101
102    if (!mysqli_stmt_fetch($stmt))
103        printf("[017] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
104
105    mysqli_stmt_close($stmt);
106
107    if (!($stmt = mysqli_stmt_init($link)) ||
108        !mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id ASC LIMIT 2") ||
109        !mysqli_stmt_execute($stmt))
110        printf("[018] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
111
112    if (!is_object($res = mysqli_stmt_get_result($stmt)) || 'mysqli_result' != get_class($res)) {
113        printf("[019] Expecting object/mysqli_result got %s/%s, [%d] %s\n",
114            gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
115    }
116
117    $id = $label = null;
118    if (!mysqli_stmt_bind_result($stmt, $id, $label))
119        printf("[020] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
120
121    $row = mysqli_fetch_assoc($res);
122    if (NULL !== $id || NULL !== $label)
123        printf("[021] Bound variables should not have been set\n");
124    mysqli_free_result($res);
125
126    mysqli_stmt_close($stmt);
127
128    if (!($stmt = mysqli_stmt_init($link)) ||
129        !mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id ASC LIMIT 2") ||
130        !mysqli_stmt_execute($stmt))
131        printf("[022] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
132
133    if (!is_object($res = mysqli_stmt_get_result($stmt)) || 'mysqli_result' != get_class($res)) {
134        printf("[023] Expecting object/mysqli_result got %s/%s, [%d] %s\n",
135            gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
136    }
137    if (!in_array($res->type, array(MYSQLI_STORE_RESULT, MYSQLI_USE_RESULT))) {
138        printf("[024] Unknown result set type %s\n", $res->type);
139    }
140    if ($res->type !== MYSQLI_STORE_RESULT)
141        printf("[025] Expecting int/%d got %s/%s", MYSQLI_STORE_RESULT, gettype($res->type), $res->type);
142
143    mysqli_free_result($res);
144    mysqli_stmt_close($stmt);
145    mysqli_close($link);
146
147    if (false !== ($res = mysqli_stmt_get_result($stmt))) {
148        printf("[026] Expecting false got %s/%s\n",
149            gettype($res), $res);
150    }
151
152        print "done!";
153?>
154--CLEAN--
155<?php
156    require_once("clean_table.inc");
157?>
158--EXPECTF--
159array(2) {
160  ["id"]=>
161  int(1)
162  ["label"]=>
163  string(1) "a"
164}
165NULL
166array(2) {
167  ["id"]=>
168  int(1)
169  ["label"]=>
170  string(1) "a"
171}
172NULL
173[017] [2014] Commands out of sync; you can't run this command now
174
175Warning: mysqli_stmt_get_result(): Couldn't fetch mysqli_stmt in %s on line %d
176done!
177