1--TEST--
2mysqli_fetch_all()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once dirname(__DIR__) . "/test_setup/test_helpers.inc";
8mysqli_check_skip_test();
9?>
10--FILE--
11<?php
12require_once dirname(__DIR__) . "/test_setup/test_helpers.inc";
13
14$link = setup_table_with_data_on_default_connection('test_mysqli_fetch_all');
15
16$result = mysqli_query($link, "SELECT * FROM test_mysqli_fetch_all ORDER BY id LIMIT 2");
17
18echo "Default mode\n";
19var_dump(mysqli_fetch_all($result));
20mysqli_free_result($result);
21
22$result = mysqli_query($link, "SELECT * FROM test_mysqli_fetch_all ORDER BY id LIMIT 2");
23print "Mode: MYSQLI_NUM\n";
24var_dump(mysqli_fetch_all($result, MYSQLI_NUM));
25mysqli_free_result($result);
26
27$result = mysqli_query($link, "SELECT * FROM test_mysqli_fetch_all ORDER BY id LIMIT 2");
28print "Mode: MYSQLI_BOTH\n";
29var_dump(mysqli_fetch_all($result, MYSQLI_BOTH));
30mysqli_free_result($result);
31
32$result = mysqli_query($link, "SELECT * FROM test_mysqli_fetch_all ORDER BY id LIMIT 2");
33print "Mode: MYSQLI_ASSOC\n";
34var_dump(mysqli_fetch_all($result, MYSQLI_ASSOC));
35
36echo "mysqli_fetch_all() after fetching\n";
37var_dump(mysqli_fetch_all($result));
38mysqli_free_result($result);
39
40$result = mysqli_query($link, "SELECT 1 AS a, 2 AS a, 3 AS c, 4 AS C, NULL AS d, true AS e");
41print "[017]\n";
42var_dump(mysqli_fetch_all($result, MYSQLI_BOTH));
43
44// Illegal mode
45try {
46    mysqli_fetch_all($result, -10);
47} catch (\ValueError $e) {
48    echo $e->getMessage() . \PHP_EOL;
49}
50mysqli_free_result($result);
51try {
52    mysqli_fetch_array($result, MYSQLI_ASSOC);
53} catch (Error $exception) {
54    echo $exception->getMessage() . "\n";
55}
56
57print "done!";
58?>
59--CLEAN--
60<?php
61require_once dirname(__DIR__) . "/test_setup/test_helpers.inc";
62tear_down_table_on_default_connection('test_mysqli_fetch_all');
63?>
64--EXPECT--
65Default mode
66array(2) {
67  [0]=>
68  array(2) {
69    [0]=>
70    string(1) "1"
71    [1]=>
72    string(1) "a"
73  }
74  [1]=>
75  array(2) {
76    [0]=>
77    string(1) "2"
78    [1]=>
79    string(1) "b"
80  }
81}
82Mode: MYSQLI_NUM
83array(2) {
84  [0]=>
85  array(2) {
86    [0]=>
87    string(1) "1"
88    [1]=>
89    string(1) "a"
90  }
91  [1]=>
92  array(2) {
93    [0]=>
94    string(1) "2"
95    [1]=>
96    string(1) "b"
97  }
98}
99Mode: MYSQLI_BOTH
100array(2) {
101  [0]=>
102  array(4) {
103    [0]=>
104    string(1) "1"
105    ["id"]=>
106    string(1) "1"
107    [1]=>
108    string(1) "a"
109    ["label"]=>
110    string(1) "a"
111  }
112  [1]=>
113  array(4) {
114    [0]=>
115    string(1) "2"
116    ["id"]=>
117    string(1) "2"
118    [1]=>
119    string(1) "b"
120    ["label"]=>
121    string(1) "b"
122  }
123}
124Mode: MYSQLI_ASSOC
125array(2) {
126  [0]=>
127  array(2) {
128    ["id"]=>
129    string(1) "1"
130    ["label"]=>
131    string(1) "a"
132  }
133  [1]=>
134  array(2) {
135    ["id"]=>
136    string(1) "2"
137    ["label"]=>
138    string(1) "b"
139  }
140}
141mysqli_fetch_all() after fetching
142array(0) {
143}
144[017]
145array(1) {
146  [0]=>
147  array(11) {
148    [0]=>
149    string(1) "1"
150    ["a"]=>
151    string(1) "2"
152    [1]=>
153    string(1) "2"
154    [2]=>
155    string(1) "3"
156    ["c"]=>
157    string(1) "3"
158    [3]=>
159    string(1) "4"
160    ["C"]=>
161    string(1) "4"
162    [4]=>
163    NULL
164    ["d"]=>
165    NULL
166    [5]=>
167    string(1) "1"
168    ["e"]=>
169    string(1) "1"
170  }
171}
172mysqli_fetch_all(): Argument #2 ($mode) must be one of MYSQLI_NUM, MYSQLI_ASSOC, or MYSQLI_BOTH
173mysqli_result object is already closed
174done!
175