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