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