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