We now have a youtube channel. Subscribe!

PHP | mysqli_send_long_data() Function

PHP mysqli_stmt_send_long_data() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the built-in mysqli_stmt_send_long_data() Function in PHP.

The built-in mysqli_stmt_send_long_data() function in PHP is used for sending data in blocks.

Allows to send parameter data to the server in pieces (or chunks), e.g. if the size of a blob exceeds the size of max_allowed_packet. This function can be called multiple times to send the parts of a character or binary data value for a column, which must be one of the TEXT or BLOB datatypes.

Syntax

Following below is the syntax to use this function -

mysqli_stmt_send_long_data($stmt);


Parameter Details

Sr.NoParameter & 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 Value

This built-in PHP function returns TRUE on success and FALSE on failure.

PHP Version

This built-in PHP function was first introduced in PHP version 5 and works in all the later versions.

Example1

The following below is an example which demonstrates the usage of the built-in PHP mysqli_stmt_send_long_data() function (in a 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);
?>

Output

When the above code is executed, it will produce the following result -

Table Created
Data Inserted

After the execution of the above code, 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)

Example2

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

Let's assume we that we have a file named foo.txt having the message "Hello how are you doing? Welcome to Webdesigntutorialz" 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();
?>

Output

When the above code is executed, it will produce the following result -

Table Created
Data Inserted

After the execution of the above code, the contents of the test table will be as follows -

mysql> select * from test;
+--------------------------------------------------------+
| message                                                |
+--------------------------------------------------------+
| Hello how are you doing? Welcome to Webdesigntutorialz |
+--------------------------------------------------------+
1 row in set (0.00 sec)


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP mysqli_stmt_store_result() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain