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