php-doc-en/reference/mysqli/mysqli_stmt/bind-param.xml
Kamil Tekiela 5c3589b9b8
Updating mysqli: bind_param (#512)
* Add mysqli_stmt_prepare to the descr.
* Simplify examples
* Fix example title
* Added new example with ...
2021-04-08 12:55:16 +02:00

242 lines
7.3 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="mysqli-stmt.bind-param" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>mysqli_stmt::bind_param</refname>
<refname>mysqli_stmt_bind_param</refname>
<refpurpose>Binds variables to a prepared statement as parameters</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<para>&style.oop;</para>
<methodsynopsis role="oop">
<modifier>public</modifier> <type>bool</type><methodname>mysqli_stmt::bind_param</methodname>
<methodparam><type>string</type><parameter>types</parameter></methodparam>
<methodparam><type>mixed</type><parameter role="reference">var</parameter></methodparam>
<methodparam rep="repeat"><type>mixed</type><parameter role="reference">vars</parameter></methodparam>
</methodsynopsis>
<para>&style.procedural;</para>
<methodsynopsis>
<type>bool</type><methodname>mysqli_stmt_bind_param</methodname>
<methodparam><type>mysqli_stmt</type><parameter>stmt</parameter></methodparam>
<methodparam><type>string</type><parameter>types</parameter></methodparam>
<methodparam><type>mixed</type><parameter role="reference">var</parameter></methodparam>
<methodparam rep="repeat"><type>mixed</type><parameter role="reference">vars</parameter></methodparam>
</methodsynopsis>
<para>
Bind variables for the parameter markers in the SQL statement prepared by
<function>mysqli_prepare</function> or <function>mysqli_stmt_prepare</function>.
</para>
<note>
<para>
If data size of a variable exceeds max. allowed packet size
(max_allowed_packet), you have to specify <literal>b</literal> in
<parameter>types</parameter> and use
<function>mysqli_stmt_send_long_data</function> to send the data in packets.
</para>
</note>
<note>
<para>
Care must be taken when using <function>mysqli_stmt_bind_param</function> in conjunction
with <function>call_user_func_array</function>. Note that <function>mysqli_stmt_bind_param</function>
requires parameters to be passed by reference, whereas <function>call_user_func_array</function>
can accept as a parameter a list of variables that can represent references or values.
</para>
</note>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
&mysqli.stmt.description;
<varlistentry>
<term><parameter>types</parameter></term>
<listitem>
<para>
A string that contains one or more characters which specify the types
for the corresponding bind variables:
<table xml:id="mysqli-stmt.bind-param.parameters">
<title>Type specification chars</title>
<tgroup cols="2">
<thead>
<row>
<entry>Character</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>i</entry>
<entry>corresponding variable has type integer</entry>
</row>
<row>
<entry>d</entry>
<entry>corresponding variable has type double</entry>
</row>
<row>
<entry>s</entry>
<entry>corresponding variable has type string</entry>
</row>
<row>
<entry>b</entry>
<entry>corresponding variable is a blob and will be sent in packets</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>var</parameter></term>
<term><parameter>vars</parameter></term>
<listitem>
<para>
The number of variables and length of string
<parameter>types</parameter> must match the parameters in the statement.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&return.success;
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><methodname>mysqli_stmt::bind_param</methodname> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);
$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;
$stmt->execute();
printf("%d row inserted.\n", $stmt->affected_rows);
/* Clean up table CountryLanguage */
$mysqli->query("DELETE FROM CountryLanguage WHERE Language='Bavarian'");
printf("%d row deleted.\n", $mysqli->affected_rows);
]]>
</programlisting>
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');
$stmt = mysqli_prepare($link, "INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'sssd', $code, $language, $official, $percent);
$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;
mysqli_stmt_execute($stmt);
printf("%d row inserted.\n", mysqli_stmt_affected_rows($stmt));
/* Clean up table CountryLanguage */
mysqli_query($link, "DELETE FROM CountryLanguage WHERE Language='Bavarian'");
printf("%d row deleted.\n", mysqli_affected_rows($link));
]]>
</programlisting>
&examples.outputs;
<screen>
<![CDATA[
1 row inserted.
1 row deleted.
]]>
</screen>
</example>
<example>
<title>Using <literal>...</literal> to provide arguments</title>
<para>
The <literal>...</literal> operator can be used to provide variable-length
argument list, e.g. in a <literal>WHERE IN</literal> clause.
</para>
<programlisting role="php">
<![CDATA[
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
$stmt = $mysqli->prepare("SELECT Language FROM CountryLanguage WHERE CountryCode IN (?, ?)");
/* Using ... to provide arguments */
$stmt->bind_param('ss', ...['DEU', 'POL']);
$stmt->execute();
$stmt->store_result();
printf("%d rows found.\n", $stmt->num_rows());
]]>
</programlisting>
&examples.outputs;
<screen>
<![CDATA[
10 rows found.
]]>
</screen>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>mysqli_stmt_bind_result</function></member>
<member><function>mysqli_stmt_execute</function></member>
<member><function>mysqli_stmt_fetch</function></member>
<member><function>mysqli_prepare</function></member>
<member><function>mysqli_stmt_send_long_data</function></member>
<member><function>mysqli_stmt_errno</function></member>
<member><function>mysqli_stmt_error</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->