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