• PHP Video Tutorials

PHP mysqli_stmt_send_long_data() Function


Advertisements

Definition and Usage

If one of the columns of table is of TEXT of BLOB type,the mysqli_stmt_send_long_data() function is used to send data to that column in chunks.

You cannot close persistent connections using this function.

Syntax

mysqli_stmt_send_long_data($stmt);

Parameters

Sr.No Parameter & Description
1

stmt(Mandatory)

This is an object representing a prepared statement.

2

param_nr(Mandatory)

This is an integer value representing the parameter to which you need to associate the given data.

3

data(Mandatory)

This is an string value representing the data to be sent.

Return Values

The PHP mysqli_stmt_send_long_data() function returns a boolean value which is true on success and false on failure.

PHP Version

This function was first introduced in PHP Version 5 and works works in all the later versions.

Example

Following example demonstrates the usage of the mysqli_stmt_send_long_data() function (in procedural style) −

<?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);
?>

This will produce following result −

Table Created
Data Inserted

After the execution of the above program the contents of the test table will be as follows −

mysql> select * from test;
+---------------------+
| message             |
+---------------------+
| This is sample data |
+---------------------+
1 row in set (0.00 sec)

Example

In object oriented style the syntax of this function is $stmt->send_long_data(); Following is the example of this function in object oriented style $minus;

Assume we have a file named foo.txt having the message Hello how are you welcome to Tutorialspoint in it.

<?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();
?>

This will produce following result −

Table Created
Data Inserted

After the execution of the above program the contents of the test table will be as follows −

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