1--TEST--
2mysqli_stmt_send_long_data()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11    require_once("connect.inc");
12
13    $tmp    = NULL;
14    $link   = NULL;
15
16    if (!is_null($tmp = @mysqli_stmt_send_long_data()))
17        printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
18
19    if (!is_null($tmp = @mysqli_stmt_send_long_data($link)))
20        printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
21
22    require('table.inc');
23
24    if (!$stmt = mysqli_stmt_init($link))
25        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
26
27    if (NULL !== ($tmp = @mysqli_stmt_send_long_data($stmt, '')))
28        printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
29
30    if (!mysqli_query($link, "DROP TABLE IF EXISTS test"))
31        printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
32
33    if (!mysqli_query($link, sprintf("CREATE TABLE test(id INT NOT NULL AUTO_INCREMENT, label LONGBLOB, PRIMARY KEY(id)) ENGINE = %s", $engine)))
34        printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
35
36    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (?, ?)"))
37        printf("[007] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
38
39    $id = null;
40    $label = null;
41    if (!mysqli_stmt_bind_param($stmt, "ib", $id, $label))
42        printf("[008] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
43
44    if (!$res = mysqli_query($link, "SHOW VARIABLES LIKE 'max_allowed_packet'"))
45        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
46
47    if (!$row = mysqli_fetch_assoc($res))
48        printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
49
50    mysqli_free_result($res);
51
52    if (isset($row['VARIABLE_VALUE']) && !isset($row['Value']))
53        // MySQL 6.0
54        $row['Value'] = $row['VARIABLE_VALUE'];
55
56    if (0 === ($max_allowed_packet = (int)$row['Value']))
57        printf("[011] Cannot determine max_allowed_packet size and/or bogus max_allowed_packet setting used.\n");
58
59    // let's ignore upper limits for LONGBLOB (2^32) ...
60    // maximum packet size up to which we test is 10M
61    $tmp = '';
62    $blob = '';
63    for ($i = 0; $i < 1024; $i++) {
64        $tmp .= 'a';
65    }
66
67    $limit = min(floor($max_allowed_packet / 1024 / 2), 10240);
68    for ($i = 0; $i < $limit; $i++)
69            $blob .= $tmp;
70    /*
71    if (floor($max_allowed_packet / 1024) <= 10240) {
72            $limit = strlen($blob) - $max_allowed_packet - 1;
73            for ($i = 0; $i < $limit; $i++)
74                    $blob .= 'a';
75    }
76    */
77    assert(strlen($blob) <= $max_allowed_packet);
78
79    if (false !== ($tmp = mysqli_stmt_send_long_data($stmt, -1, $blob)))
80        printf("[012] Expecting boolean/false, got %s/%s. [%d] %s\n",
81            gettype($tmp), $tmp, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
82
83    if (NULL !== ($tmp = @mysqli_stmt_send_long_data($stmt, PHP_INT_MAX + 1, $blob)))
84        printf("[013] Expecting NULL, got %s/%s. [%d] %s\n",
85            gettype($tmp), $tmp, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
86
87    if (false !== ($tmp = mysqli_stmt_send_long_data($stmt, 999, $blob)))
88        printf("[014] Expecting boolean/false, got %s/%s. [%d] %s\n",
89            gettype($tmp), $tmp, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
90
91    if (true !== ($tmp = mysqli_stmt_send_long_data($stmt, 1, $blob)))
92        printf("[015] Expecting boolean/true, got %s/%s. [%d] %s\n",
93            gettype($tmp), $tmp, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
94
95    $id = 1;
96    if (true !== mysqli_stmt_execute($stmt))
97        printf("[016] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
98    mysqli_stmt_close($stmt);
99
100    if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id"))
101        printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
102
103    if (1 != ($tmp = mysqli_num_rows($res)))
104        printf("[018] Expecting 1 rows, mysqli_num_rows() reports %d rows. [%d] %s\n",
105            $tmp, mysqli_errno($link), mysqli_error($link));
106
107    if (!$row = mysqli_fetch_assoc($res))
108        printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
109
110    mysqli_free_result($res);
111
112    if (empty($row['id']) || empty($row['label']) || ($row['id'] != 1))
113        printf("[020] Record seems to be incomplete\n");
114
115    if ($blob != $row['label'])
116        printf("[021] Blob value has not been stored properly!\n");
117
118    if (NULL !== ($tmp = @mysqli_stmt_send_long_data($stmt, '')))
119        printf("[022] Expecting NULL, got %s/%s\n");
120
121    /* Check that the function alias exists. It's a deprecated function,
122    but we have not announce the removal so far, therefore we need to check for it */
123    if (!is_null($tmp = @mysqli_stmt_send_long_data()))
124        printf("[023] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
125
126    mysqli_close($link);
127    print "done!";
128?>
129--CLEAN--
130<?php
131    require_once("clean_table.inc");
132?>
133--EXPECTF--
134Warning: mysqli_stmt_send_long_data(): Invalid parameter number in %s on line %d
135done!
136