mysqli_stmt_send_long_data()函数分块发送数据。
如果表的某一列是BLOB类型的TEXT,则该mysqli_stmt_send_long_data()函数用于将数据分块发送到该列。
您不能使用此函数关闭持久连接。
mysqli_stmt_send_long_data($stmt);
| 序号 | 参数及说明 |
|---|---|
| 1 | stmt(必需) 这是表示准备好的语句的对象。 |
| 2 | param_nr(必需) 这是一个整数值,表示您需要将给定数据关联到的参数。 |
| 3 | data(必需) 这是一个字符串值,表示要发送的数据。 |
PHP mysqli_stmt_send_long_data()函数返回一个布尔值,成功时为true,失败时为false。
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_stmt_send_long_data()函数的用法(面向过程风格)-
<?php
//建立连接
$con = mysqli_connect("localhost", "root", "password", "mydb");
//创建表
mysqli_query($con, "CREATE TABLE test(message BLOB)");
print("创建表 \n");
//插入数据
$stmt = mysqli_prepare($con, "INSERT INTO test values(?)");
//将值绑定到参数标记
mysqli_stmt_bind_param($stmt, "b", $txt);
$txt = NULL;
$data = "This is sample data";
mysqli_stmt_send_long_data($stmt, 0, $data);
print("插入数据");
//执行语句
mysqli_stmt_execute($stmt);
//结束语句
mysqli_stmt_close($stmt);
//关闭连接
mysqli_close($con);
?>输出结果
创建表 插入数据
执行完上述程序后,测试表的内容如下:
mysql> select * from test; +---------------------+ | message | +---------------------+ | This is sample data | +---------------------+ 1 row in set (0.00 sec)
在面向对象风格中,此函数的语法为$stmt-> send_long_data();。以下是面向对象风格中此函数的示例;
假设我们有一个名为foo.txt的文件,内容为“Hello how are you welcome to nhooo.com”。
<?php
//建立连接
$con = new mysqli("localhost", "root", "password", "mydb");
//创建表
$con -> query("CREATE TABLE test(message BLOB)");
print("创建表 \n");
//使用预准备语句将值插入到表中
$stmt = $con -> prepare("INSERT INTO test values(?)");
//将值绑定到参数标记
$txt = NULL;
$stmt->bind_param("b", $txt);
$fp = fopen("foo.txt", "r");
while (!feof($fp)) {
$stmt->send_long_data( 0, fread($fp, 8192));
}
print("插入数据");
fclose($fp);
//执行语句
$stmt->execute();
//结束语句
$stmt->close();
//关闭连接
$con->close();
?>输出结果
创建表 插入数据
执行完上述程序后,测试表的内容如下:
mysql> select * from test; +---------------------------------------------+ | message | +---------------------------------------------+ | Hello how are you welcome to nhooo.com | +---------------------------------------------+ 1 row in set (0.00 sec)