1--TEST--
2mysqli_stmt_bind_result()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11	require_once("connect.inc");
12
13	function dec32bin($dec, $bits) {
14
15		$maxval = pow(2, $bits);
16		$bin = '';
17		for ($bitval = $maxval; $bitval >= 1; $bitval = $bitval / 2) {
18			if (($dec / $bitval) >= 1) {
19				$bin .= '1';
20				$dec -= $bitval;
21			} else {
22				$bin .= '0';
23			}
24		}
25		return $bin;
26	}
27
28	if (!$link_ins = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
29		printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
30			$host, $user, $db, $port, $socket);
31
32	if (!$link_sel = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
33		printf("[002] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
34			$host, $user, $db, $port, $socket);
35
36	for ($bits = 1; $bits <= 46; $bits++) {
37
38		if (1 == $bits)
39			$max_value = 1;
40		else
41			$max_value = pow(2, $bits) - 1;
42
43		if (!mysqli_query($link_ins, "DROP TABLE IF EXISTS test")) {
44			printf("[003 - %d] [%d] %s\n", $bits, mysqli_errno($link_ins), mysqli_error($link_ins));
45		}
46
47		if (!mysqli_query($link_ins, sprintf("CREATE TABLE test(id BIGINT, bit_value BIT(%d) NOT NULL, bit_null BIT(%d) DEFAULT NULL) ENGINE = %s", $bits, $bits, $engine))) {
48			// don't bail - column type might not be supported by the server, ignore this
49			continue;
50		}
51		if (!$stmt_ins = mysqli_stmt_init($link_ins)) {
52			printf("[004 - %d] [%d] %s\n", $bits, mysqli_errno($link_ins), mysqli_error($link_ins));
53			continue;
54		}
55
56		if (!mysqli_stmt_prepare($stmt_ins, "INSERT INTO test(id, bit_value) VALUES (?, ?)")) {
57			printf("[005 - %d] [%d] %s\n", $bits, mysqli_stmt_errno($stmt_ins), mysqli_stmt_error($stmt_ins));
58			mysqli_stmt_close($stmt_ins);
59			continue;
60		}
61
62		if (!($stmt_sel = mysqli_stmt_init($link_sel))) {
63			printf("[006 - %d] [%d] %s\n", $bits, mysqli_errno($link_sel), mysqli_error($link_sel));
64			mysqli_stmt_close($stmt_ins);
65				continue;
66		}
67
68		$tests = 0;
69		$rand_max = mt_getrandmax();
70		while ($tests < 10) {
71
72			$tests++;
73			if (1 == $tests)
74				$value = 0;
75			else if (2 == $tests)
76				$value = $max_value;
77			else {
78				if ($max_value > $rand_max) {
79					$max_loops = floor($max_value/$rand_max);
80					$num_loops = mt_rand(1, $max_loops);
81					$value = 0;
82					for ($i = 0; $i < $num_loops; $i++)
83						$value += mt_rand(0, $rand_max);
84				} else {
85					$value = mt_rand(0, $max_value);
86				}
87			}
88
89			$bin = ($bits < 32) ? decbin($value) : dec32bin($value, $bits);
90			for ($i = 0; ($i < strlen($bin)) && ($bin[$i] == '0'); $i++)
91				;
92			$bin2 = substr($bin, $i, strlen($bin));
93
94			if (!mysqli_stmt_bind_param($stmt_ins, "dd", $value, $value)) {
95				printf("[007 - %d] [%d] %s\n", $bits, mysqli_stmt_errno($stmt_ins), mysqli_stmt_error($stmt_ins));
96				mysqli_stmt_close($stmt_ins);
97				continue;
98			}
99			if (!mysqli_stmt_execute($stmt_ins)) {
100				printf("[008 - %d] [%d] %s\n", $bits, mysqli_stmt_errno($stmt_ins), mysqli_stmt_error($stmt_ins));
101				break;
102			}
103			$sql = sprintf("SELECT id, BIN(bit_value) AS _bin, bit_value, bit_value + 0 AS _bit_value0, bit_null FROM test WHERE id = %s", $value);
104			if ((!mysqli_stmt_prepare($stmt_sel, $sql)) ||
105				(!mysqli_stmt_execute($stmt_sel))) {
106				printf("[009 - %d] [%d] %s\n", $bits, mysqli_stmt_errno($stmt_sel), mysqli_stmt_error($stmt_sel));
107				break;
108			}
109
110			$row = array('id' => -1, '_bin' => - 1, 'bit_value' => -1, '_bit_value0' => -1, 'bit_null' => -1);
111			if (!mysqli_stmt_bind_result($stmt_sel, $row['id'], $row['_bin'], $row['bit_value'], $row['_bit_value0'], $row['bit_null'])) {
112				printf("[010 - %d] [%d] %s\n", $bits, mysqli_stmt_errno($stmt_sel), mysqli_stmt_error($stmt_sel));
113				break;
114			}
115
116			if (!($ret = mysqli_stmt_fetch($stmt_sel))) {
117				printf("[011 - %d] mysqli_stmt_fetch() has failed for %d bits - ret = %s/%s, [%d] %s, [%d] %s\n",
118					$bits, $bits,
119					gettype($ret), $ret,
120					mysqli_stmt_errno($stmt_sel), mysqli_stmt_error($stmt_sel),
121					mysqli_errno($link_sel), mysqli_errno($link_sel)
122				);
123				break;
124			}
125
126			if (($value != $row['id']) || (($bin != $row['_bin']) && ($bin2 != $row['_bin']))) {
127				debug_zval_dump($row);
128				printf("[012 - %d] Insert of %s in BIT(%d) column might have failed. id = %s, bin = %s (%s/%s)\n",
129					$bits, $value, $bits, $row['id'], $row['_bin'], $bin, $bin2);
130				break;
131			}
132			if ($value != $row['bit_value']) {
133				debug_zval_dump($row);
134				printf("[013 - %d] Expecting %s got %s\n", $bits, $value, $row['bit_value']);
135				break;
136			}
137
138			if (null !== $row['bit_null']) {
139				debug_zval_dump($row);
140				printf("[014 - %d] Expecting null got %s/%s\n", $bits, gettype($row['bit_value']), $row['bit_value']);
141				break;
142			}
143
144		}
145
146		mysqli_stmt_close($stmt_ins);
147		mysqli_stmt_close($stmt_sel);
148	}
149
150	mysqli_close($link_ins);
151	mysqli_close($link_sel);
152	print "done!";
153?>
154--CLEAN--
155<?php
156	require_once("clean_table.inc");
157?>
158--EXPECTF--
159done!