The mysqli_stmt::send_long_data() function is used to send a large amount of data to the binding parameters in the preprocessing statement. It is mainly used to send data of BLOB or TEXT types, because these types of data may take up a lot of space in memory.
Syntax: bool mysqli_stmt::send_long_data(int $param_nr, string $data)
parameter:
Return value: Return true if data is sent successfully; otherwise return false.
Example: Suppose there is a database table named "employees" with a BLOB type field "resume" in which we want to save a resume file to this field.
// 连接数据库$mysqli = new mysqli("localhost", "username", "password", "database"); // 准备预处理语句$stmt = $mysqli->prepare("INSERT INTO employees (resume) VALUES (?)"); // 打开文件并读取数据$file = fopen("resume.pdf", "rb"); $data = fread($file, filesize("resume.pdf")); fclose($file); // 绑定参数并发送数据$stmt->bind_param("b", $resumeData); $resumeData = $data; $stmt->send_long_data(1, $resumeData); // 执行预处理语句$stmt->execute(); // 检查是否成功插入数据if ($stmt->affected_rows > 0) { echo "简历插入成功!"; } else { echo "插入失败!"; } // 关闭连接$stmt->close(); $mysqli->close();
In the example above, we first open and read the resume file to be inserted, and then bind the data to the parameters in the preprocessing statement. Next, use the send_long_data() function to send the data to the server. Finally, execute the preprocessing statement and check if the data is inserted successfully.
Note that the send_long_data() function must be called after the bind_param() function and must be called before the execute() function. In addition, if you want to send a large amount of data of multiple parameters, you can call the send_long_data() function multiple times, sending the data of one parameter at a time.