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