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