1--TEST--
2mysqli_stmt_send_long_data()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7if (getenv("SKIP_SLOW_TESTS")) die('skip slow test');
8require_once 'skipifconnectfailure.inc';
9?>
10--FILE--
11<?php
12    require 'table.inc';
13
14    if (!$stmt = mysqli_stmt_init($link))
15        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
16
17    if (!mysqli_query($link, "DROP TABLE IF EXISTS test"))
18        printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
19
20    if (!mysqli_query($link, sprintf("CREATE TABLE test(id INT NOT NULL AUTO_INCREMENT, label LONGBLOB, PRIMARY KEY(id)) ENGINE = %s", $engine)))
21        printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
22
23    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (?, ?)"))
24        printf("[007] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
25
26    $id = null;
27    $label = null;
28    if (!mysqli_stmt_bind_param($stmt, "ib", $id, $label))
29        printf("[008] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
30
31    if (!$res = mysqli_query($link, "SHOW VARIABLES LIKE 'max_allowed_packet'"))
32        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
33
34    if (!$row = mysqli_fetch_assoc($res))
35        printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
36
37    mysqli_free_result($res);
38
39    if (isset($row['VARIABLE_VALUE']) && !isset($row['Value']))
40        // MySQL 6.0
41        $row['Value'] = $row['VARIABLE_VALUE'];
42
43    if (0 === ($max_allowed_packet = (int)$row['Value']))
44        printf("[011] Cannot determine max_allowed_packet size and/or bogus max_allowed_packet setting used.\n");
45
46    // let's ignore upper limits for LONGBLOB (2^32) ...
47    // maximum packet size up to which we test is 10M
48    $tmp = '';
49    $blob = '';
50    for ($i = 0; $i < 1024; $i++) {
51        $tmp .= 'a';
52    }
53
54    $limit = min(floor($max_allowed_packet / 1024 / 2), 10240);
55    for ($i = 0; $i < $limit; $i++)
56            $blob .= $tmp;
57    /*
58    if (floor($max_allowed_packet / 1024) <= 10240) {
59            $limit = strlen($blob) - $max_allowed_packet - 1;
60            for ($i = 0; $i < $limit; $i++)
61                    $blob .= 'a';
62    }
63    */
64    assert(strlen($blob) <= $max_allowed_packet);
65
66    try {
67        mysqli_stmt_send_long_data($stmt, -1, $blob);
68    } catch (\ValueError $e) {
69        echo $e->getMessage() . \PHP_EOL;
70    }
71
72    if (false !== ($tmp = mysqli_stmt_send_long_data($stmt, 999, $blob)))
73        printf("[014] Expecting boolean/false, got %s/%s. [%d] %s\n",
74            gettype($tmp), $tmp, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
75
76    if (true !== ($tmp = mysqli_stmt_send_long_data($stmt, 1, $blob)))
77        printf("[015] Expecting boolean/true, got %s/%s. [%d] %s\n",
78            gettype($tmp), $tmp, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
79
80    $id = 1;
81    if (true !== mysqli_stmt_execute($stmt))
82        printf("[016] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
83    mysqli_stmt_close($stmt);
84
85    if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id"))
86        printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
87
88    if (1 != ($tmp = mysqli_num_rows($res)))
89        printf("[018] Expecting 1 rows, mysqli_num_rows() reports %d rows. [%d] %s\n",
90            $tmp, mysqli_errno($link), mysqli_error($link));
91
92    if (!$row = mysqli_fetch_assoc($res))
93        printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
94
95    mysqli_free_result($res);
96
97    if (empty($row['id']) || empty($row['label']) || ($row['id'] != 1))
98        printf("[020] Record seems to be incomplete\n");
99
100    if ($blob != $row['label'])
101        printf("[021] Blob value has not been stored properly!\n");
102
103    mysqli_close($link);
104    print "done!";
105?>
106--CLEAN--
107<?php
108    require_once 'clean_table.inc';
109?>
110--EXPECT--
111mysqli_stmt_send_long_data(): Argument #2 ($param_num) must be greater than or equal to 0
112done!
113