1--TEST--
2INSERT and packet overflow
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8?>
9--INI--
10memory_limit=256M
11--FILE--
12<?php
13    require_once 'connect.inc';
14    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
15        printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
16
17    if (!$res = mysqli_query($link, "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'"))
18        printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
19
20    if (!$row = mysqli_fetch_assoc($res))
21        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
22
23    mysqli_free_result($res);
24
25    if (0 === ($org_max_allowed_packet = (int)$row['Value']))
26        printf("[004] Cannot determine max_allowed_packet size and/or bogus max_allowed_packet setting used.\n");
27
28    $max_len = pow(2, 24);
29    if ($org_max_allowed_packet < $max_len) {
30        if (!mysqli_query($link, "SET GLOBAL max_allowed_packet = " . ($max_len + 100))) {
31            if (1227 == mysqli_errno($link)) {
32                /* [1227] Access denied; you need the SUPER privilege for this operation */
33                print "done!";
34                exit(0);
35            } else {
36                printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
37            }
38        }
39    }
40    mysqli_close($link);
41    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
42        printf("[006] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
43
44    if (!mysqli_query($link, "SET NAMES 'latin1'"))
45        printf("[007] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
46
47    if (!$res = mysqli_query($link, "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'"))
48        printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
49
50    if (!$row = mysqli_fetch_assoc($res))
51        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
52
53    mysqli_free_result($res);
54
55    if (0 === ($max_allowed_packet = (int)$row['Value']))
56        printf("[010] Cannot determine max_allowed_packet size and/or bogus max_allowed_packet setting used.\n");
57
58    $max_len = pow(2, 24);
59    if ($max_allowed_packet < $max_len) {
60        printf("[011] Failed to change max_allowed_packet");
61    }
62
63    $table_name = 'test__mysqli_insert_packet_overflow';
64    if (!mysqli_query($link, "DROP TABLE IF EXISTS {$table_name}")) {
65        printf("[012] Failed to drop old test table: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
66    }
67
68    if (!mysqli_query($link, "CREATE TABLE {$table_name}(col_blob LONGBLOB) ENGINE=" . $engine))
69        printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
70
71    $query_prefix = "INSERT INTO {$table_name}(col_blob) VALUES ('";
72    $query_postfix = "')";
73    $query_len = strlen($query_prefix) + strlen($query_postfix);
74    $com_query_len = 2;
75
76
77    $blob = str_repeat('a', $max_len - $com_query_len - $query_len);
78    $query = sprintf("%s%s%s", $query_prefix, $blob, $query_postfix);
79
80    if (!mysqli_query($link, $query))
81        printf("[014] max_allowed_packet = %d, strlen(query) = %d, [%d] %s\n", $max_allowed_packet, strlen($query), mysqli_errno($link), mysqli_error($link));
82
83    if (!$res = mysqli_query($link, "SELECT col_blob FROM {$table_name}"))
84        printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
85
86    if (!$row = mysqli_fetch_assoc($res)) {
87        printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
88    } else {
89        if ($row['col_blob'] != $blob) {
90            printf("[017] Blob seems wrong, dumping data\n");
91            var_dump(strlen($row['col_blob']));
92            var_dump(strlen($blob));
93        }
94        mysqli_free_result($res);
95    }
96
97    if (!mysqli_query($link, "SET GLOBAL max_allowed_packet = " . $org_max_allowed_packet))
98        if (1227 != mysqli_errno($link))
99            printf("[018] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
100
101    mysqli_close($link);
102
103    print "done!";
104?>
105--CLEAN--
106<?php
107require_once 'connect.inc';
108$link = new mysqli($host, $user, $passwd, $db, $port, $socket);
109$link->query('DROP TABLE test__mysqli_insert_packet_overflow');
110$link->close();
111?>
112--EXPECT--
113done!
114