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