1--TEST--
2Bind limits
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 bind_many($offset, $link, $num_params, $rows, $eval = true) {
14
15		$drop = "DROP TABLE IF EXISTS test";
16		$create = "CREATE TABLE test(id INT AUTO_INCREMENT PRIMARY KEY, ";
17		$insert = "INSERT INTO test";
18		$columns = "";
19		$values = "";
20		$stmt_params = "";
21		$params = array();
22		for ($i = 0; $i < $num_params; $i++) {
23			$create 		.= "col" . $i . " INT, ";
24			$columns 		.= "col" . $i . ", ";
25			$values 		.= "?, ";
26			$stmt_params 	.= '$params[' . $i . '], ';
27			for ($j = 0; $j < $rows; $j++)
28			  $params[($j * $rows) + $i] = $i;
29		}
30		$create = substr($create, 0, -2) . ")";
31
32		$stmt_types = str_repeat("i", $num_params * $rows);
33		$stmt_params = substr(str_repeat($stmt_params, $rows), 0, -2);
34		$values = substr($values, 0, -2);
35		$insert .= "(" . substr($columns, 0, -2) . ") VALUES ";
36		$insert .= substr(str_repeat("(" . $values . "), ", $rows), 0, -2);
37
38		$stmt_bind_param = 'return mysqli_stmt_bind_param($stmt, "' . $stmt_types . '", ' . $stmt_params . ');';
39
40		printf("Testing %d columns with %d rows...\n", $num_params, $rows);
41
42		if (!$link->query($drop) || !$link->query($create)) {
43			printf("[%03d + 01] [%d] %s\n", $offset, $link->errno, $link->error);
44			return false;
45		}
46		printf("... table created\n");
47
48		if (!$stmt = $link->prepare($insert)) {
49			printf("[%03d + 02] [%d] %s\n", $offset, $link->errno, $link->error);
50			return false;
51		}
52		if ($stmt->param_count != $num_params * $rows) {
53			  printf("[%03d + 03] Parameter count should be %d but got %d\n", $offset, $num_params * $rows, $stmt->param_count);
54			return false;
55		}
56		printf("... statement with %d parameters prepared\n", $stmt->param_count);
57
58		if ($eval) {
59			if (!eval($stmt_bind_param)) {
60				printf("[%03d + 03] [%d] %s\n", $offset, $stmt->errno, $stmt->error);
61				return false;
62			}
63		} else {
64			$param_ref = array($stmt_types);
65			for ($i = 0; $i < $rows; $i++)
66				for ($j = 0; $j < $num_params; $j++)
67					$param_ref[] = &$params[($i * $rows) + $j];
68
69			if (!call_user_func_array(array($stmt, 'bind_param'), $param_ref)) {
70				printf("[%03d + 03] [%d] %s\n", $offset, $stmt->errno, $stmt->error);
71				return false;
72			}
73		}
74		if ($stmt->param_count != $num_params * $rows) {
75			 printf("[%03d + 03] Parameter count should be %d but got %d\n", $offset, $num_params * $rows, $stmt->param_count);
76			return false;
77		}
78
79		if (!$stmt->execute()) {
80			printf("[%03d + 04] [%d] %s\n", $offset, $stmt->errno, $stmt->error);
81			return false;
82		}
83		printf("Statement done\n");
84
85		$stmt->close();
86
87		if (!($res = $link->query("SELECT * FROM test"))) {
88			printf("[%03d + 05] [%d] %s\n", $offset, $link->errno, $link->error);
89			return false;
90		}
91
92		$row = $res->fetch_row();
93		$res->close();
94
95		for ($i = 0; $i < $num_params; $i++) {
96			if ($row[$i + 1] != $i) {
97				printf("[%03d + 06] [%d] %s\n", $offset, $link->errno, $link->error);
98			}
99		}
100
101		return true;
102	}
103
104	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
105		printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
106			$host, $user, $db, $port, $socket);
107	}
108
109	var_dump(bind_many(10, $link, 273, 240, true));
110	var_dump(bind_many(20, $link, 273, 240, false));
111	mysqli_close($link);
112	print "done!";
113?>
114--CLEAN--
115<?php
116require_once("clean_table.inc");
117?>
118--EXPECTF--
119Testing 273 columns with 240 rows...
120... table created
121... statement with 65520 parameters prepared
122Statement done
123bool(true)
124Testing 273 columns with 240 rows...
125... table created
126... statement with 65520 parameters prepared
127Statement done
128bool(true)
129done!
130