xref: /PHP-8.1/ext/mysqli/tests/bug52891.phpt (revision b5a14e6c)
1--TEST--
2Bug #52891 (Wrong data inserted with mysqli/mysqlnd when using bind_param,value > LONG_MAX)
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once('skipifconnectfailure.inc');
8if (!$IS_MYSQLND) {
9    die("skip: test applies only to mysqlnd");
10}
11?>
12--FILE--
13<?php
14    require_once("connect.inc");
15
16    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
17        printf("[001] Connect failed, [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
18    }
19
20    if (!$link->query("DROP TABLE IF EXISTS tuint") ||
21        !$link->query("DROP TABLE IF EXISTS tsint")) {
22        printf("[002] [%d] %s\n", $link->errno, $link->error);
23    }
24
25    if (!$link->query("CREATE TABLE tuint(a BIGINT UNSIGNED) ENGINE=" . $engine) ||
26        !$link->query("CREATE TABLE tsint(a BIGINT) ENGINE=" . $engine)) {
27        printf("[003] [%d] %s\n", $link->errno, $link->error);
28    }
29
30
31    if (!$stmt1 = $link->prepare("INSERT INTO tuint VALUES(?)"))
32        printf("[004] [%d] %s\n", $link->errno, $link->error);
33
34    if (!$stmt2 = $link->prepare("INSERT INTO tsint VALUES(?)"))
35        printf("[005] [%d] %s\n", $link->errno, $link->error);
36
37    $param = 42;
38
39    if (!$stmt1->bind_param("i", $param))
40        printf("[006] [%d] %s\n", $stmt1->errno, $stmt1->error);
41
42    if (!$stmt2->bind_param("i", $param))
43        printf("[007] [%d] %s\n", $stmt2->errno, $stmt2->error);
44
45    /* first insert normal value to force initial send of types */
46    if (!$stmt1->execute())
47        printf("[008] [%d] %s\n", $stmt1->errno, $stmt1->error);
48
49    if	(!$stmt2->execute())
50        printf("[009] [%d] %s\n", $stmt2->errno, $stmt2->error);
51
52    /* now try values that don't fit in long, on 32bit, new types should be sent or 0 will be inserted */
53    $param = -4294967297;
54    if (!$stmt2->execute())
55        printf("[010] [%d] %s\n", $stmt2->errno, $stmt2->error);
56
57    /* again normal value */
58    $param = 43;
59
60    if (!$stmt1->execute())
61        printf("[011] [%d] %s\n", $stmt1->errno, $stmt1->error);
62
63    if	(!$stmt2->execute())
64        printf("[012] [%d] %s\n", $stmt2->errno, $stmt2->error);
65
66    /* again conversion */
67    $param = -4294967295;
68    if (!$stmt2->execute())
69        printf("[013] [%d] %s\n", $stmt2->errno, $stmt2->error);
70
71    $param = 4294967295;
72    if (!$stmt1->execute())
73        printf("[014] [%d] %s\n", $stmt1->errno, $stmt1->error);
74
75    if	(!$stmt2->execute())
76        printf("[015] [%d] %s\n", $stmt2->errno, $stmt2->error);
77
78    $param = 4294967297;
79    if (!$stmt1->execute())
80        printf("[016] [%d] %s\n", $stmt1->errno, $stmt1->error);
81
82    if	(!$stmt2->execute())
83        printf("[017] [%d] %s\n", $stmt2->errno, $stmt2->error);
84
85    $result = $link->query("SELECT * FROM tsint ORDER BY a ASC");
86    $result2 = $link->query("SELECT * FROM tuint ORDER BY a ASC");
87
88    echo "tsint:\n";
89    while ($row = $result->fetch_assoc()) {
90        var_dump($row);
91    }
92    echo "tuint:\n";
93    while ($row = $result2->fetch_assoc()) {
94        var_dump($row);
95    }
96
97    echo "done";
98?>
99--CLEAN--
100<?php
101require_once('connect.inc');
102
103if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
104    printf("[clean] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
105        $host, $user, $db, $port, $socket);
106}
107
108if (!mysqli_query($link, 'DROP TABLE IF EXISTS tuint')) {
109    printf("[clean] Failed to drop old test table: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
110}
111
112if (!mysqli_query($link, 'DROP TABLE IF EXISTS tsint')) {
113    printf("[clean] Failed to drop old test table: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
114}
115
116mysqli_close($link);
117?>
118--EXPECT--
119tsint:
120array(1) {
121  ["a"]=>
122  string(11) "-4294967297"
123}
124array(1) {
125  ["a"]=>
126  string(11) "-4294967295"
127}
128array(1) {
129  ["a"]=>
130  string(2) "42"
131}
132array(1) {
133  ["a"]=>
134  string(2) "43"
135}
136array(1) {
137  ["a"]=>
138  string(10) "4294967295"
139}
140array(1) {
141  ["a"]=>
142  string(10) "4294967297"
143}
144tuint:
145array(1) {
146  ["a"]=>
147  string(2) "42"
148}
149array(1) {
150  ["a"]=>
151  string(2) "43"
152}
153array(1) {
154  ["a"]=>
155  string(10) "4294967295"
156}
157array(1) {
158  ["a"]=>
159  string(10) "4294967297"
160}
161done
162