PHP: Hypertext Preprocessor (original) (raw)

mysqli_stmt::send_long_data

mysqli_stmt_send_long_data

(PHP 5, PHP 7, PHP 8)

mysqli_stmt::send_long_data -- mysqli_stmt_send_long_data — Send data in blocks

Description

Object-oriented style

public mysqli_stmt::send_long_data(int $param_num, string $data): bool

Parameters

statement

Procedural style only: A mysqli_stmt object returned by mysqli_stmt_init().

param_num

Indicates which parameter to associate the data with. Parameters are numbered beginning with 0.

data

A string containing data to be sent.

Return Values

Returns [true](reserved.constants.php#constant.true) on success or [false](reserved.constants.php#constant.false) on failure.

Examples

Example #1 Object-oriented style

<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">stmt = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6151em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">m</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>mysqli->prepare("INSERT INTO messages (message) VALUES (?)"); $null = NULL; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>−</mo><mo>&gt;</mo><mi>b</mi><mi>i</mi><mi>n</mi><msub><mi>d</mi><mi>p</mi></msub><mi>a</mi><mi>r</mi><mi>a</mi><mi>m</mi><mo stretchy="false">(</mo><mi mathvariant="normal">&quot;</mi><mi>b</mi><mi mathvariant="normal">&quot;</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">stmt-&gt;bind_param(&quot;b&quot;, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6984em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">m</span><span class="mord mathnormal">t</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0361em;vertical-align:-0.2861em;"></span><span class="mord mathnormal">bin</span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">p</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">am</span><span class="mopen">(</span><span class="mord">&quot;</span><span class="mord mathnormal">b</span><span class="mord">&quot;</span><span class="mpunct">,</span></span></span></span>null); $fp = fopen("messages.txt", "r"); while (!feof($fp)) { <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>−</mo><mo>&gt;</mo><mi>s</mi><mi>e</mi><mi>n</mi><msub><mi>d</mi><mi>l</mi></msub><mi>o</mi><mi>n</mi><msub><mi>g</mi><mi>d</mi></msub><mi>a</mi><mi>t</mi><mi>a</mi><mo stretchy="false">(</mo><mn>0</mn><mo separator="true">,</mo><mi>f</mi><mi>r</mi><mi>e</mi><mi>a</mi><mi>d</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">stmt-&gt;send_long_data(0, fread(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6984em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">m</span><span class="mord mathnormal">t</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">se</span><span class="mord mathnormal">n</span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.01968em;">l</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">o</span><span class="mord mathnormal">n</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0359em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">d</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal">a</span><span class="mopen">(</span><span class="mord">0</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">re</span><span class="mord mathnormal">a</span><span class="mord mathnormal">d</span><span class="mopen">(</span></span></span></span>fp, 8192)); } fclose($fp); $stmt->execute(); ?>

See Also

Found A Problem?

ChrisH

5 years ago

`If you are trying to write a single field which is above max_allowed_packet then this function will not help you (contrary to what the documentation example seems to show above).

Parameters in MySQL are still restricted by max_allowed_packet on a per-field basis so you will get an error like:

“mysqli_sql_exception: Parameter of prepared statement which is set through mysql_send_long_data() is longer than 'max_long_data_size' bytes”

The only real use case for this function seems to be if you are writing multiple long fields which when combined would go over max_allowed_packet.

`

Gustavo Narea

18 years ago

DimeCadmium

4 years ago

To ChrisH's note, you must call this function multiple times with the same $param_nr, to send the first max_allowed_packet bytes, then the next, and so on. So you might need to do a for loop over changing substr() indexes, or etc.