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