1--TEST--
2mysqli_stmt_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    if (!$stmt = mysqli_stmt_init($link))
16        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
17
18    function func_test_mysqli_stmt_num_rows($stmt, $query, $expected, $offset) {
19
20        if (!mysqli_stmt_prepare($stmt, $query)) {
21            printf("[%03d] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
22            return false;
23        }
24
25        if (!mysqli_stmt_execute($stmt)) {
26            printf("[%03d] [%d] %s\n", $offset + 1, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
27            return false;
28        }
29
30        if (!mysqli_stmt_store_result($stmt)) {
31            printf("[%03d] [%d] %s\n", $offset + 2, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
32            return false;
33        }
34
35        if ($expected !== ($tmp = mysqli_stmt_num_rows($stmt)))
36            printf("[%03d] Expecting %s/%d, got %s/%d\n", $offset + 3,
37                gettype($expected), $expected,
38                gettype($tmp), $tmp);
39
40        mysqli_stmt_free_result($stmt);
41
42        return true;
43    }
44
45    func_test_mysqli_stmt_num_rows($stmt, "SELECT 1 AS a", 1, 10);
46    func_test_mysqli_stmt_num_rows($stmt, "SHOW VARIABLES LIKE '%nixnutz%'", 0, 20);
47    // Note: for statements that return no result set mysqli_num_rows() differs from mysqli_stmt_num_rows() slightly
48    // mysqli_num_rows() failed to fetch the result set and the PHP parameter check makes it return NULL
49    // mysqli_stmt_numrows() has a valid resource to work on and it will return int/0 instead. No bug, but
50    // slightly different behaviour... - if you really check the data types and don't rely on casting like 98% of all PHP
51    // users do.
52    func_test_mysqli_stmt_num_rows($stmt, "INSERT INTO test(id, label) VALUES (100, 'z')", 0, 30);
53
54    if ($res = mysqli_query($link, 'SELECT COUNT(id) AS num FROM test')) {
55        $row = mysqli_fetch_assoc($res);
56        mysqli_free_result($res);
57        func_test_mysqli_stmt_num_rows($stmt, "SELECT id, label FROM test", (int)$row['num'], 40);
58    } else {
59        printf("[050] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
60    }
61
62    print "run_tests.php don't fool me with your 'ungreedy' expression '.+?'!\n";
63
64    if (!mysqli_stmt_prepare($stmt, 'SELECT id FROM test'))
65        printf("[051] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
66
67    if (mysqli_stmt_execute($stmt)) {
68
69        $i = 0;
70        do {
71            if (0 !== ($tmp = mysqli_stmt_num_rows($stmt)))
72                printf("[53 - %03d] Expecting int/0, got %s/%s\n", $i, gettype($tmp), $tmp);
73            $i++;
74        } while (mysqli_stmt_fetch($stmt));
75
76        /* NOTE to users
77        Behaviour with libmysql is UNDEFINED, see http://news.php.net/php.internals/55210
78        Because it is undefined it is allowed to the mysqlnd DEVELOPER to implement
79        any behaviour they like, including the one checked for in this test.
80        What the test does is cover an implementation detail of the mysqlnd library.
81        This implementation detail may, at any time, change without prior notice.
82        On the contrary, the mysqlnd way is a reasonable one and, maybe, one fine
83        day, after Klingons visited earth, becomes the official one. Meanwhile, do
84        not rely on it.
85        */
86        if (7 !== ($tmp = mysqli_stmt_num_rows($stmt)))
87            printf("[54] Expecting int/7, got %s/%s\n", gettype($tmp), $tmp);
88
89    } else {
90        printf("[055] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
91    }
92
93    mysqli_stmt_close($stmt);
94
95    try {
96        mysqli_stmt_num_rows($stmt);
97    } catch (Error $exception) {
98        echo $exception->getMessage() . "\n";
99    }
100
101    mysqli_close($link);
102    print "done!";
103?>
104--CLEAN--
105<?php
106    require_once("clean_table.inc");
107?>
108--EXPECT--
109run_tests.php don't fool me with your 'ungreedy' expression '.+?'!
110mysqli_stmt object is already closed
111done!
112