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