<?php
// Reproduce bug that with send_long_data, execute() fails
// Agjust your settings before execute
$conn = new mysqli(
'localhost', // << Server
'root', // << Username
'root', // << Password
'test' // << Schema
);
if (!$conn->query('CREATE TABLE IF NOT EXISTS `test_bug_blob` (`id` integer auto_increment, `data` BLOB, PRIMARY KEY(`id`));'))
die("Error creating table.\n");
if (!($result = $conn->query('SELECT @@max_allowed_packet')))
die("Error reading max allowed packet size.\n");
$max_allowed_packet = $result->fetch_array();
$max_allowed_packet = $max_allowed_packet[0];
if (!($stmt = $conn->prepare('INSERT INTO `test_bug_blob` (`data`) VALUES (?)')))
die("Cannot prepare statement for INSERT. {$conn->error}\n");
// Sent blob smaller than max allowed_packet
$data = str_repeat('0123456789', $max_allowed_packet/20);
if (!$stmt->bind_param('s', $data))
die("Error binding parameters. {$stmt->error}\n");
if (!$stmt->execute())
die("Error executing prepared statement. {$stmt->error}\n");
echo "OK: Executed prepared statement with blob less than max_allowed_packet.\n";
// Sent blob bigger than max allowed_packet
$big_data = str_repeat('0123456789', $max_allowed_packet/9);
$null = null;
if (!$stmt->bind_param('b', $null))
die("Error binding parameters. {$stmt->error}\n");
foreach(str_split($big_data, $max_allowed_packet) as $packet )
if (!$stmt->send_long_data(0, $packet))
die("Error sending long packet. {$stmt->error}\n");
if (!$stmt->execute())
die("Error executing prepared statement. {$stmt->error}\n");
echo "OK: Executed prepared statement with blob bigger than max_allowed_packet, sent at chunks.\n";