1--TEST--
2mysqli_stmt_bind_result() - ZEROFILL
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--FILE--
9<?php
10    require_once('connect.inc');
11    require_once('table.inc');
12
13    function zerofill($offset, $link, $datatype, $insert = 1) {
14
15        mysqli_query($link, 'ALTER TABLE test DROP zero');
16        $sql = sprintf('ALTER TABLE test ADD zero %s UNSIGNED ZEROFILL', $datatype);
17        if (!mysqli_query($link, $sql)) {
18            // no worries - server might not support it
19            return true;
20        }
21
22        if (!mysqli_query($link, sprintf('UPDATE test SET zero = %s', $insert))) {
23            printf("[%03d] UPDATE failed, [%d] %s\n",
24                $offset, mysqli_errno($link), mysqli_error($link));
25            return false;
26        }
27
28        if (!($stmt = mysqli_prepare($link, 'SELECT zero FROM test LIMIT 1'))) {
29            printf("[%03d] SELECT failed, [%d] %s\n",
30                $offset, mysqli_errno($link), mysqli_error($link));
31            return false;
32        }
33
34        $result = null;
35        if (!mysqli_stmt_bind_result($stmt, $result)) {
36            printf("[%03d] Bind failed, [%d] %s\n",
37                mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
38            return false;
39        }
40
41        if (!mysqli_stmt_execute($stmt) || !mysqli_stmt_fetch($stmt)) {
42            printf("[%03d] Execute or fetch failed, [%d] %s\n",
43                mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
44            return false;
45        }
46
47        $res = mysqli_stmt_result_metadata($stmt);
48        $meta = mysqli_fetch_fields($res);
49        mysqli_stmt_free_result($stmt);
50
51        $meta = $meta[0];
52        $length = $meta->length;
53        if ($length > strlen($insert)) {
54
55            $expected = str_repeat('0', $length - strlen($insert));
56            $expected .= $insert;
57            if ($expected !== $result) {
58                printf("[%03d] Expecting '%s' got '%s'\n", $offset, $expected, $result);
59                return false;
60            }
61
62        } else if ($length <= 1) {
63            printf("[%03d] Length reported is too small to run test\n", $offset);
64            return false;
65        }
66
67
68        return true;
69    }
70
71    /*
72    We map those to PHP numeric types -
73        no padding/filling done. Neither with libmysql nor with mysqlnd.
74    zerofill(2, $link, 'TINYINT');
75    zerofill(3, $link, 'SMALLINT');
76    zerofill(4, $link, 'MEDIUMINT');
77    zerofill(5, $link, 'INT');
78    zerofill(6, $link, 'INTEGER');
79    zerofill(7, $link, 'BIGINT');
80    zerofill(8, $link, 'FLOAT');
81    zerofill(9, $link, 'DOUBLE');
82    zerofill(10, $link, 'DOUBLE PRECISION');
83    */
84    zerofill(11, $link, 'DECIMAL');
85    zerofill(12, $link, 'DEC');
86
87    mysqli_close($link);
88
89    print "done!";
90?>
91--CLEAN--
92<?php
93    require_once("clean_table.inc");
94?>
95--EXPECT--
96done!
97