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