1--TEST--
2mysqli_num_rows()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11    require_once("connect.inc");
12
13    require('table.inc');
14
15    function func_test_mysqli_num_rows($link, $query, $expected, $offset, $test_free = false) {
16
17        if (!$res = mysqli_query($link, $query, MYSQLI_STORE_RESULT)) {
18            printf("[%03d] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
19            return;
20        }
21
22        if (!is_bool($res)) {
23            if ($expected !== ($tmp = mysqli_num_rows($res)))
24                printf("[%03d] Expecting %s/%d, got %s/%d\n", $offset + 1,
25                    gettype($expected), $expected,
26                    gettype($tmp), $tmp);
27
28            mysqli_free_result($res);
29
30            try {
31                mysqli_num_rows($res);
32            } catch (Error $exception) {
33                echo $exception->getMessage() . "\n";
34            }
35        }
36    }
37
38    func_test_mysqli_num_rows($link, "SELECT 1 AS a", 1, 5);
39    func_test_mysqli_num_rows($link, "SHOW VARIABLES LIKE '%nixnutz%'", 0, 10);
40    func_test_mysqli_num_rows($link, "INSERT INTO test(id, label) VALUES (100, 'z')", NULL, 15);
41    func_test_mysqli_num_rows($link, "SELECT id FROM test LIMIT 2", 2, 20, true);
42
43    if ($res = mysqli_query($link, 'SELECT COUNT(id) AS num FROM test')) {
44
45        $row = mysqli_fetch_assoc($res);
46        mysqli_free_result($res);
47
48        func_test_mysqli_num_rows($link, "SELECT id, label FROM test", (int)$row['num'], 25);
49
50    } else {
51        printf("[030] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
52    }
53
54    print "run_tests.php don't fool me with your 'ungreedy' expression '.+?'!\n";
55
56    if ($res = mysqli_query($link, 'SELECT id FROM test', MYSQLI_USE_RESULT)) {
57
58        $row = mysqli_fetch_row($res);
59        try {
60            var_dump(mysqli_num_rows($res));
61        } catch (\Error $e) {
62            echo $e->getMessage() . \PHP_EOL;
63        }
64
65        mysqli_free_result($res);
66    } else {
67        printf("[032] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
68    }
69
70    mysqli_close($link);
71    print "done!";
72?>
73--CLEAN--
74<?php
75    require_once("clean_table.inc");
76?>
77--EXPECT--
78mysqli_result object is already closed
79mysqli_result object is already closed
80mysqli_result object is already closed
81mysqli_result object is already closed
82run_tests.php don't fool me with your 'ungreedy' expression '.+?'!
83mysqli_num_rows() cannot be used in MYSQLI_USE_RESULT mode
84done!
85