mysqli_stmt_send_long_data()函數(shù)分塊發(fā)送數(shù)據(jù)。
如果表的某一列是BLOB類型的TEXT,則該mysqli_stmt_send_long_data()函數(shù)用于將數(shù)據(jù)分塊發(fā)送到該列。
您不能使用此函數(shù)關(guān)閉持久連接。
mysqli_stmt_send_long_data($stmt);
| 序號 | 參數(shù)及說明 |
|---|---|
| 1 | stmt(必需) 這是表示準(zhǔn)備好的語句的對象。 |
| 2 | param_nr(必需) 這是一個整數(shù)值,表示您需要將給定數(shù)據(jù)關(guān)聯(lián)到的參數(shù)。 |
| 3 | data(必需) 這是一個字符串值,表示要發(fā)送的數(shù)據(jù)。 |
PHP mysqli_stmt_send_long_data()函數(shù)返回一個布爾值,成功時為true,失敗時為false。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_stmt_send_long_data()函數(shù)的用法(面向過程風(fēng)格)-
<?php
//建立連接
$con = mysqli_connect("localhost", "root", "password", "mydb");
//創(chuàng)建表
mysqli_query($con, "CREATE TABLE test(message BLOB)");
print("創(chuàng)建表 \n");
//插入數(shù)據(jù)
$stmt = mysqli_prepare($con, "INSERT INTO test values(?)");
//將值綁定到參數(shù)標(biāo)記
mysqli_stmt_bind_param($stmt, "b", $txt);
$txt = NULL;
$data = "This is sample data";
mysqli_stmt_send_long_data($stmt, 0, $data);
print("插入數(shù)據(jù)");
//執(zhí)行語句
mysqli_stmt_execute($stmt);
//結(jié)束語句
mysqli_stmt_close($stmt);
//關(guān)閉連接
mysqli_close($con);
?>輸出結(jié)果
創(chuàng)建表 插入數(shù)據(jù)
執(zhí)行完上述程序后,測試表的內(nèi)容如下:
mysql> select * from test; +---------------------+ | message | +---------------------+ | This is sample data | +---------------------+ 1 row in set (0.00 sec)
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語法為$stmt-> send_long_data();。以下是面向?qū)ο箫L(fēng)格中此函數(shù)的示例;
假設(shè)我們有一個名為foo.txt的文件,內(nèi)容為“Hello how are you welcome to (cainiaoplus.com)”。
<?php
//建立連接
$con = new mysqli("localhost", "root", "password", "mydb");
//創(chuàng)建表
$con -> query("CREATE TABLE test(message BLOB)");
print("創(chuàng)建表 \n");
//使用預(yù)準(zhǔn)備語句將值插入到表中
$stmt = $con -> prepare("INSERT INTO test values(?)");
//將值綁定到參數(shù)標(biāo)記
$txt = NULL;
$stmt->bind_param("b", $txt);
$fp = fopen("foo.txt", "r");
while (!feof($fp)) {
$stmt->send_long_data( 0, fread($fp, 8192));
}
print("插入數(shù)據(jù)");
fclose($fp);
//執(zhí)行語句
$stmt->execute();
//結(jié)束語句
$stmt->close();
//關(guān)閉連接
$con->close();
?>輸出結(jié)果
創(chuàng)建表 插入數(shù)據(jù)
執(zhí)行完上述程序后,測試表的內(nèi)容如下:
mysql> select * from test; +---------------------------------------------+ | message | +---------------------------------------------+ | Hello how are you welcome to (cainiaoplus.com) | +---------------------------------------------+ 1 row in set (0.00 sec)