PHP mysqli_stmt_send_long_data() 函数



定义和用法

如果表的其中一列是TEXT或BLOB类型,则使用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 版本

此函数首次引入于PHP 5版本,并在所有后续版本中有效。

示例

以下示例演示了mysqli_stmt_send_long_data()函数的用法(过程式风格)−

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Creating a table
   mysqli_query($con, "CREATE TABLE test(message BLOB)");
   print("Table Created \n");

   //Inserting data
   $stmt = mysqli_prepare($con, "INSERT INTO test values(?)");

   //Binding values to the parameter markers
   mysqli_stmt_bind_param($stmt, "b", $txt);
   $txt = NULL;

   $data = "This is sample data";

   mysqli_stmt_send_long_data($stmt, 0, $data);
   print("Data Inserted");

   //Executing the statement
   mysqli_stmt_execute($stmt);
   //Closing the statement
   mysqli_stmt_close($stmt);
   //Closing the connection
   mysqli_close($con);
?>

这将产生以下结果−

Table Created
Data Inserted

程序执行后,test表的內容如下−

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 Tutorialspoint

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   //Creating a table
   $con -> query("CREATE TABLE test(message BLOB)");
   print("Table Created \n");

   //Inserting values into the table using prepared statement
   $stmt = $con -> prepare("INSERT INTO test values(?)");

   //Binding values to the parameter markers
   $txt = NULL;
   $stmt->bind_param("b", $txt);

   $fp = fopen("foo.txt", "r");
   while (!feof($fp)) {
      $stmt->send_long_data( 0, fread($fp, 8192));
   }
   print("Data Inserted");
   fclose($fp);

   //Executing the statement
   $stmt->execute();
   //Closing the statement
   $stmt->close();
   //Closing the connection
   $con->close();
?>

这将产生以下结果−

Table Created
Data Inserted

程序执行后,test表的內容如下−

mysql> select * from test;
+---------------------------------------------+
| message                                     |
+---------------------------------------------+
| Hello how are you welcome to Tutorialspoint |
+---------------------------------------------+
1 row in set (0.00 sec)
php_function_reference.htm
广告