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
廣告