1--TEST--
2mysqli_stmt_get_result()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8?>
9--FILE--
10<?php
11    /*
12    NOTE: no datatype tests here! This is done by
13    mysqli_stmt_bind_result.phpt already. Restrict
14    this test case to the basics.
15    */
16    require 'table.inc';
17
18    if (!$stmt = mysqli_stmt_init($link))
19        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
20
21    // stmt object status test
22    try {
23        mysqli_stmt_fetch($stmt);
24    } catch (Error $exception) {
25        echo $exception->getMessage() . "\n";
26    }
27
28    if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id LIMIT 2"))
29        printf("[005] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
30
31    // FIXME - different versions return different values ?!
32    if ((NULL !== ($tmp = mysqli_stmt_get_result($stmt))) && (false !== $tmp))
33        printf("[006] Expecting NULL or boolean/false, got %s/%s\n", gettype($tmp), var_export($tmp, 1));
34
35    if (!mysqli_stmt_execute($stmt))
36        printf("[007] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
37
38    if (!mysqli_stmt_store_result($stmt))
39        printf("[008] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
40
41    if (is_object($tmp = mysqli_stmt_store_result($stmt)))
42        printf("[009] non-object, got %s/%s\n", gettype($tmp), var_export($tmp, 1));
43
44    mysqli_stmt_close($stmt);
45
46    if (!$stmt = mysqli_stmt_init($link))
47        printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
48
49    // stmt object status test
50    try {
51        mysqli_stmt_fetch($stmt);
52    } catch (Error $exception) {
53        echo $exception->getMessage() . "\n";
54    }
55
56    if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id LIMIT 2"))
57        printf("[012] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
58
59    // FIXME - different versions return different values ?!
60    if ((NULL !== ($tmp = mysqli_stmt_get_result($stmt))) && (false !== $tmp))
61        printf("[013] Expecting NULL or boolean/false, got %s/%s\n", gettype($tmp), var_export($tmp, 1));
62
63    if (!mysqli_stmt_execute($stmt))
64        printf("[014] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
65
66    if (!is_object($tmp = mysqli_stmt_get_result($stmt)))
67        printf("[016] NULL, got %s/%s\n", gettype($tmp), var_export($tmp, 1));
68
69    mysqli_free_result($tmp);
70    mysqli_stmt_close($stmt);
71
72    if (!$stmt = mysqli_stmt_init($link))
73        printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
74
75    // stmt object status test
76    try {
77        mysqli_stmt_get_result($stmt);
78    } catch (Error $exception) {
79        echo $exception->getMessage() . "\n";
80    }
81
82    if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id LIMIT 2"))
83        printf("[019] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
84
85    if (false !== ($tmp = mysqli_stmt_get_result($stmt)))
86        printf("[020] Expecting false, got %s/%s\n", gettype($tmp), var_export($tmp, 1));
87
88    if (!mysqli_stmt_execute($stmt))
89        printf("[023] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
90
91    if (!is_object($tmp = mysqli_stmt_get_result($stmt)))
92        printf("[024] Expecting object, got %s/%s\n", gettype($tmp), var_export($tmp, 1));
93
94    if (false !== ($tmp = mysqli_stmt_fetch($stmt)))
95        printf("[025] false, got %s/%s\n", gettype($tmp), var_export($tmp, 1));
96
97    if (true !== ($tmp = mysqli_stmt_bind_result($stmt, $id, $label))) {
98        printf("[026] [%d] [%s]\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
99        printf("[027] [%d] [%s]\n", mysqli_errno($link), mysqli_error($link));
100        printf("[028] Expecting boolean/true, got %s/%s\n", gettype($tmp), var_export($tmp, 1));
101    }
102
103    if (false !== ($tmp = mysqli_stmt_fetch($stmt)))
104        printf("[029] false, got %s/%s\n", gettype($tmp), var_export($tmp, 1));
105
106    mysqli_stmt_close($stmt);
107
108    // get_result can be used in PS cursor mode
109    if (!$stmt = mysqli_stmt_init($link))
110        printf("[030] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
111
112    if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id LIMIT 2"))
113        printf("[031] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
114
115    if (!mysqli_stmt_attr_set($stmt, MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_READ_ONLY))
116        printf("[032] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
117
118    if (!mysqli_stmt_execute($stmt))
119        printf("[033] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
120
121    $result = mysqli_stmt_get_result($stmt);
122    while ($row = mysqli_fetch_assoc($result)) {
123        var_dump($row);
124    }
125
126    if (!$stmt = mysqli_stmt_init($link))
127        printf("[034] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
128
129    if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id LIMIT 2"))
130        printf("[035] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
131
132    if (!mysqli_stmt_execute($stmt))
133        printf("[036] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
134
135    $id = NULL;
136    $label = NULL;
137    if (true !== ($tmp = mysqli_stmt_bind_result($stmt, $id, $label)))
138        printf("[037] Expecting boolean/true, got %s/%s\n", gettype($tmp), var_export($tmp, 1));
139
140    if (!is_object($tmp = $result = mysqli_stmt_get_result($stmt)))
141        printf("[038] Expecting array, got %s/%s, [%d] %s\n",
142            gettype($tmp), var_export($tmp, 1),
143            mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
144
145    if (false !== ($tmp = mysqli_stmt_fetch($stmt)))
146        printf("[039] Expecting boolean/false, got %s/%s, [%d] %s\n",
147            gettype($tmp), $tmp, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
148
149    printf("[040] [%d] [%s]\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
150    printf("[041] [%d] [%s]\n", mysqli_errno($link), mysqli_error($link));
151    while ($row = mysqli_fetch_assoc($result)) {
152        var_dump($row);
153    }
154    mysqli_free_result($result);
155
156	if (!mysqli_kill($link, mysqli_thread_id($link)))
157		printf("[042] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
158
159	if (false !== ($tmp = mysqli_stmt_get_result($stmt)))
160		printf("[043] Expecting false, got %s/%s\n", gettype($tmp), var_export($tmp, 1));
161
162	mysqli_stmt_close($stmt);
163
164	try {
165        mysqli_stmt_fetch($stmt);
166    } catch (Error $exception) {
167        echo $exception->getMessage(), "\n";
168    }
169
170	mysqli_close($link);
171
172	print "done!";
173?>
174--CLEAN--
175<?php
176    require_once 'clean_table.inc';
177?>
178--EXPECTF--
179mysqli_stmt object is not fully initialized
180mysqli_stmt object is not fully initialized
181mysqli_stmt object is not fully initialized
182array(2) {
183  ["id"]=>
184  int(1)
185  ["label"]=>
186  string(1) "a"
187}
188array(2) {
189  ["id"]=>
190  int(2)
191  ["label"]=>
192  string(1) "b"
193}
194[040] [2014] [Commands out of sync; you can't run this command now]
195[041] [0] []
196array(2) {
197  ["id"]=>
198  int(1)
199  ["label"]=>
200  %s(1) "a"
201}
202array(2) {
203  ["id"]=>
204  int(2)
205  ["label"]=>
206  %s(1) "b"
207}
208mysqli_stmt object is already closed
209done!
210