mysqli_stmt::send_long_data must be called before executing the statement. If you try to use send_long_data after calling execute(), an error will occur. Common error messages include:
Warning: mysqli_stmt::send_long_data() expects parameter 1 to be resource, null given.
This error occurs because the SQL statement handle was not properly prepared when send_long_data was called.
Solution
Make sure to use send_long_data before executing the SQL statement. The correct order should be:
<span>$stmt = $mysqli->prepare("INSERT INTO large_data (content) VALUES (?)");
$stmt->bind_param("s", $data);
$stmt->send_long_data(0, $largeData); // send large data
$stmt->execute(); // execute the statement
</span>
Problem Description
send_long_data requires the correct parameter index. For example, if the SQL statement has multiple placeholders, you must provide the correct parameter index.
Warning: mysqli_stmt::send_long_data() expects parameter 1 to be resource, null given.
This error usually occurs when the wrong index is specified, preventing PHP from locating the data correctly.
Solution
Make sure the index in send_long_data is correct. Indexes start from 0, representing the first placeholder in the SQL statement. For example:
<span>$stmt = $mysqli->prepare("INSERT INTO large_data (content) VALUES (?)");
$stmt->bind_param("s", $data);
$stmt->send_long_data(0, $largeData); // correctly send long data for the first placeholder
$stmt->execute();
</span>
Problem Description
mysqli_stmt::send_long_data is used to handle large data, but if the data exceeds the maximum size allowed by MySQL configuration, the transfer will fail. This usually appears as the following error:
MySQL Error: Data too long for column.
This error is typically related to the max_allowed_packet parameter in the MySQL configuration file, which controls the maximum size of a single data packet.
Solution
Check and adjust the max_allowed_packet parameter in the MySQL configuration file. You can view the current setting with the following command:
SHOW VARIABLES LIKE 'max_allowed_packet';
If the value is too small, increase it in my.cnf or my.ini and restart MySQL:
max_allowed_packet = 64M
Problem Description
When using send_long_data to send large data, the data type must match the placeholder type in the SQL statement. For example, if the placeholder type is BLOB, the data must be binary.
If the data type does not match, errors may occur, such as:
Warning: mysqli_stmt::send_long_data() expects parameter 2 to be string, resource given.
Solution
Ensure the data type matches the SQL placeholder type. If the placeholder is a string (s), the data should be a string. If it’s binary (b), the data should be binary.
<span>$stmt = $mysqli->prepare("INSERT INTO large_data (file_content) VALUES (?)");
$stmt->bind_param("b", $binaryData);
$stmt->send_long_data(0, $binaryData); // send binary data
$stmt->execute();
</span>
Problem Description
send_long_data requires an active database connection. If the connection times out or is closed during execution, errors may occur. Possible error messages include:
Error: Lost connection to MySQL server during query.
This usually happens when the database is idle for too long or the connection is interrupted during large data uploads.
Solution
To avoid timeouts, ensure the connection stays alive during the operation. If data transfer takes a long time, adjust PHP’s max_execution_time or MySQL’s wait_timeout.
ini_set('max_execution_time', 300); // set PHP max execution time to 5 minutes
You can also process large data in chunks instead of sending everything at once to prevent connection loss.
Problem Description
All parameters must be bound correctly when executing SQL statements. If parameter binding fails, send_long_data may not work properly. For example, if bind_param was used but the data was not successfully bound, issues will occur.
Solution
Make sure all required parameters are bound using bind_param before execution, and check the return value to confirm successful binding.
<span>$stmt = $mysqli->prepare("INSERT INTO large_data (content) VALUES (?)");
$stmt->bind_param("s", $data);
if ($stmt->execute()) {
$stmt->send_long_data(0, $largeData); // send long data after binding parameters
}
</span>
Related Tags:
mysqli_stmt