1--TEST--
2mysqli_stmt_field_counts()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--FILE--
9<?php
10    require_once("connect.inc");
11
12    require('table.inc');
13
14    $stmt = mysqli_stmt_init($link);
15
16    try {
17        mysqli_stmt_field_count($stmt);
18    } catch (Error $exception) {
19        echo $exception->getMessage() . "\n";
20    }
21
22    if (mysqli_stmt_prepare($stmt, ''))
23        printf("[004] Prepare should fail for an empty statement\n");
24
25    try {
26        mysqli_stmt_field_count($stmt);
27    } catch (Error $exception) {
28        echo $exception->getMessage() . "\n";
29    }
30
31    if (!mysqli_stmt_prepare($stmt, 'SELECT 1'))
32        printf("[006] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
33    if (1 !== ($tmp = mysqli_stmt_field_count($stmt)))
34        printf("[007] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
35
36    if (!mysqli_stmt_prepare($stmt, 'SELECT 1, 2'))
37        printf("[008] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
38    if (2 !== ($tmp = mysqli_stmt_field_count($stmt)))
39        printf("[009] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
40
41    if (!mysqli_stmt_prepare($stmt, 'SELECT id, label FROM test'))
42        printf("[010] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
43    if (2 !== ($tmp = mysqli_stmt_field_count($stmt)))
44        printf("[011] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
45
46    if (!mysqli_stmt_prepare($stmt, 'SELECT label FROM test') ||
47        !mysqli_stmt_execute($stmt))
48        printf("[012] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
49    if (1 !== ($tmp = mysqli_stmt_field_count($stmt)))
50        printf("[013] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
51
52    $label = null;
53    try {
54        if (mysqli_stmt_bind_param($stmt, "s", $label))
55            printf("[014] expected error - got ok\n");
56    } catch (\ArgumentCountError $e) {
57        echo $e->getMessage() . \PHP_EOL;
58    }
59    while (mysqli_stmt_fetch($stmt))
60        if (1 !== ($tmp = mysqli_stmt_field_count($stmt)))
61            printf("[015] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
62
63    if (!mysqli_stmt_prepare($stmt, 'INSERT INTO test(id) VALUES (100)'))
64        printf("[016] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
65    if (0 !== ($tmp = mysqli_stmt_field_count($stmt)))
66        printf("[017] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
67
68    if (!mysqli_stmt_prepare($stmt, "UPDATE test SET label = 'z' WHERE id = 1") ||
69        !mysqli_stmt_execute($stmt))
70        printf("[018] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
71
72    if (0 !== ($tmp = mysqli_stmt_field_count($stmt)))
73        printf("[019] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
74
75    mysqli_stmt_close($stmt);
76
77    try {
78        mysqli_stmt_prepare($stmt, 'SELECT id FROM test');
79    } catch (Error $exception) {
80        echo $exception->getMessage() . "\n";
81    }
82
83    try {
84        mysqli_stmt_field_count($stmt);
85    } catch (Error $exception) {
86        echo $exception->getMessage() . "\n";
87    }
88
89    mysqli_close($link);
90
91    print "done!";
92?>
93--CLEAN--
94<?php
95    require_once("clean_table.inc");
96?>
97--EXPECT--
98mysqli_stmt object is not fully initialized
99mysqli_stmt object is not fully initialized
100The number of variables must match the number of parameters in the prepared statement
101mysqli_stmt object is already closed
102mysqli_stmt object is already closed
103done!
104