Sends a chunk of binary or text data to a placeholder bound with type b.
The objective is to understand the behavior and operational limits, not only the method name.
What It Does
Sends a chunk of binary or text data to a placeholder bound with type b.
Syntax and Return Value
mysqli_stmt::send_long_data(...)
The return value and exceptional cases depend on the call described here: Placeholder numbering starts at zero; bind the parameter as b before streaming chunks.
Requirements: install and enable the corresponding PHP driver or extension and configure the service before running the examples.
Example 1: Basic Use
The query assumes users(id,email) in a MySQL database; send_long_data additionally assumes attachments(data LONGBLOB).
<?php
$host = getenv('DB_HOST'); $user = getenv('DB_USER'); $password = getenv('DB_PASSWORD'); $database = getenv('DB_NAME');
if (!$host || !$user || $password === false || !$database) throw new RuntimeException('Missing database configuration');
$mysqli = new mysqli($host, $user, $password, $database);
$mysqli->set_charset('utf8mb4');
$userId = 3;
$stmt = $mysqli->prepare("SELECT id, email FROM users WHERE id >= ? ORDER BY id");
$stmt->bind_param("i", $userId);
$stmt = $mysqli->prepare("INSERT INTO attachments(data) VALUES (?)");
$blob = null;
$stmt->bind_param("b", $blob);
$source = fopen(__FILE__, "rb");
while (!feof($source)) { $part = fread($source, 4096); if ($part !== "") $stmt->send_long_data(0, $part); }
fclose($source);
$stmt->execute();
Example 2: A Second Scenario
The query assumes users(id,email) in a MySQL database; send_long_data additionally assumes attachments(data LONGBLOB).
<?php
$host = getenv('DB_HOST'); $user = getenv('DB_USER'); $password = getenv('DB_PASSWORD'); $database = getenv('DB_NAME');
if (!$host || !$user || $password === false || !$database) throw new RuntimeException('Missing database configuration');
$mysqli = new mysqli($host, $user, $password, $database);
$mysqli->set_charset('utf8mb4');
$userId = 5;
$stmt = $mysqli->prepare("SELECT id, email FROM users WHERE id >= ? ORDER BY id");
$stmt->bind_param("i", $userId);
$stmt = $mysqli->prepare("INSERT INTO attachments(data) VALUES (?)");
$blob = null;
$stmt->bind_param("b", $blob);
$source = fopen(__FILE__, "rb");
while (!feof($source)) { $part = fread($source, 4096); if ($part !== "") $stmt->send_long_data(0, $part); }
fclose($source);
$stmt->execute();
Example 3: Practical Checks
The query assumes users(id,email) in a MySQL database; send_long_data additionally assumes attachments(data LONGBLOB).
<?php
$host = getenv('DB_HOST'); $user = getenv('DB_USER'); $password = getenv('DB_PASSWORD'); $database = getenv('DB_NAME');
if (!$host || !$user || $password === false || !$database) throw new RuntimeException('Missing database configuration');
$mysqli = new mysqli($host, $user, $password, $database);
$mysqli->set_charset('utf8mb4');
$userId = 8;
$stmt = $mysqli->prepare("SELECT id, email FROM users WHERE id >= ? ORDER BY id");
$stmt->bind_param("i", $userId);
$stmt = $mysqli->prepare("INSERT INTO attachments(data) VALUES (?)");
$blob = null;
$stmt->bind_param("b", $blob);
$source = fopen(__FILE__, "rb");
while (!feof($source)) { $part = fread($source, 4096); if ($part !== "") $stmt->send_long_data(0, $part); }
fclose($source);
$stmt->execute();
Important Behavior and Edge Cases
Placeholder numbering starts at zero; bind the parameter as b before streaming chunks.
For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.
Comparison with Related Approaches
| Approach | Important distinction |
|---|---|
| mysqli_stmt::send_long_data | Sends a chunk of binary or text data to a placeholder bound with type b. |
| Related API / pattern | bind_param sends ordinary values directly, while send_long_data streams large values in chunks. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | Placeholder numbering starts at zero; bind the parameter as b before streaming chunks. |
| Using a related API interchangeably | bind_param sends ordinary values directly, while send_long_data streams large values in chunks. |
Key Takeaways
- Sends a chunk of binary or text data to a placeholder bound with type b.
- Placeholder numbering starts at zero; bind the parameter as b before streaming chunks.
- bind_param sends ordinary values directly, while send_long_data streams large values in chunks.
Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.