uTools-Manuals/docs/php/db2_bind_param.html
2019-04-08 23:22:26 +08:00

282 lines
21 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Binds a PHP variable to an SQL statement parameter</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><div id="function.db2-bind-param" class="refentry">
<div class="refnamediv">
<h1 class="refname">db2_bind_param</h1>
<p class="verinfo">(PECL ibm_db2 &gt;= 1.0.0)</p><p class="refpurpose"><span class="refname">db2_bind_param</span> &mdash; <span class="dc-title">
Binds a PHP variable to an SQL statement parameter
</span></p>
</div>
<div class="refsect1 description" id="refsect1-function.db2-bind-param-description">
<h3 class="title">说明</h3>
<div class="methodsynopsis dc-description">
<span class="methodname"><strong>db2_bind_param</strong></span>
( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$stmt</code></span>
, <span class="methodparam"><span class="type">int</span> <code class="parameter">$parameter_number</code></span>
, <span class="methodparam"><span class="type">string</span> <code class="parameter">$variable_name</code></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$parameter_type</code></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$data_type</code><span class="initializer"> = 0</span></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$precision</code><span class="initializer"> = -1</span></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$scale</code><span class="initializer"> = 0</span></span>
]]]] ) : <span class="type">bool</span></div>
<p class="para rdfs-comment">
Binds a PHP variable to an SQL statement parameter in a statement resource
returned by <span class="function"><a href="db2_prepare.html" class="function">db2_prepare()</a></span>. This function gives you more
control over the parameter type, data type, precision, and scale for the
parameter than simply passing the variable as part of the optional input
array to <span class="function"><a href="db2_execute.html" class="function">db2_execute()</a></span>.
</p>
</div>
<div class="refsect1 parameters" id="refsect1-function.db2-bind-param-parameters">
<h3 class="title">参数</h3>
<p class="para">
<dl>
<dt>
<code class="parameter">stmt</code></dt>
<dd>
<p class="para">
A prepared statement returned from <span class="function"><a href="db2_prepare.html" class="function">db2_prepare()</a></span>.
</p>
</dd>
<dt>
<code class="parameter">parameter_number</code></dt>
<dd>
<p class="para">
Specifies the 1-indexed position of the parameter in the prepared
statement.
</p>
</dd>
<dt>
<code class="parameter">variable_name</code></dt>
<dd>
<p class="para">
A string specifying the name of the PHP variable to bind to the
parameter specified by <code class="parameter">parameter_number</code>.
</p>
</dd>
<dt>
<code class="parameter">parameter_type</code></dt>
<dd>
<p class="para">
A constant specifying whether the PHP variable should be bound to the
SQL parameter as an input parameter (<em>DB2_PARAM_IN</em>),
an output parameter (<em>DB2_PARAM_OUT</em>), or as a
parameter that accepts input and returns output
(<em>DB2_PARAM_INOUT</em>). To avoid memory overhead, you can
also specify <em>DB2_PARAM_FILE</em> to bind the PHP variable
to the name of a file that contains large object (BLOB, CLOB, or DBCLOB)
data.
</p>
</dd>
<dt>
<code class="parameter">data_type</code></dt>
<dd>
<p class="para">
A constant specifying the SQL data type that the PHP variable should be
bound as: one of <em>DB2_BINARY</em>,
<em>DB2_CHAR</em>, <em>DB2_DOUBLE</em>, or
<em>DB2_LONG</em> .
</p>
</dd>
<dt>
<code class="parameter">precision</code></dt>
<dd>
<p class="para">
Specifies the precision with which the variable should be bound to the
database. This parameter can also be used for retrieving XML output values
from stored procedures. A non-negative value specifies the maximum size of
the XML data that will be retrieved from the database. If this parameter
is not used, a default of 1MB will be assumed for retrieving the XML
output value from the stored procedure.
</p>
</dd>
<dt>
<code class="parameter">scale</code></dt>
<dd>
<p class="para">
Specifies the scale with which the variable should be bound to the
database.
</p>
</dd>
</dl>
</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-function.db2-bind-param-returnvalues">
<h3 class="title">返回值</h3>
<p class="para">
成功时返回 <strong><code>TRUE</code></strong> 或者在失败时返回 <strong><code>FALSE</code></strong>
</p>
</div>
<div class="refsect1 examples" id="refsect1-function.db2-bind-param-examples">
<h3 class="title">范例</h3>
<p class="para">
<div class="example" id="example-1230">
<p><strong>Example #1 Binding PHP variables to a prepared statement</strong></p>
<div class="example-contents"><p>
The SQL statement in the following example uses two input parameters in
the WHERE clause. We call <span class="function"><strong>db2_bind_param()</strong></span> to bind two
PHP variables to the corresponding SQL parameters. Notice that the PHP
variables do not have to be declared or assigned before the call to
<span class="function"><strong>db2_bind_param()</strong></span>; in the example,
<em>$lower_limit</em> is assigned a value before the call to
<span class="function"><strong>db2_bind_param()</strong></span>, but <em>$upper_limit</em>
is assigned a value after the call to
<span class="function"><strong>db2_bind_param()</strong></span>. The variables must be bound and, for
parameters that accept input, must have any value assigned, before calling
<span class="function"><a href="db2_execute.html" class="function">db2_execute()</a></span>.
</p></div>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$sql&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;name,&nbsp;breed,&nbsp;weight&nbsp;FROM&nbsp;animals<br />&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;weight&nbsp;&gt;&nbsp;?&nbsp;AND&nbsp;weight&nbsp;&lt;&nbsp;?'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$database</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;We&nbsp;can&nbsp;declare&nbsp;the&nbsp;variable&nbsp;before&nbsp;calling&nbsp;db2_bind_param()<br /></span><span style="color: #0000BB">$lower_limit&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">db2_bind_param</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"lower_limit"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">DB2_PARAM_IN</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">db2_bind_param</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"upper_limit"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">DB2_PARAM_IN</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;We&nbsp;can&nbsp;also&nbsp;declare&nbsp;the&nbsp;variable&nbsp;after&nbsp;calling&nbsp;db2_bind_param()<br /></span><span style="color: #0000BB">$upper_limit&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">15.0</span><span style="color: #007700">;<br /><br />if&nbsp;(</span><span style="color: #0000BB">db2_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #007700">{</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]}</span><span style="color: #DD0000">,&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]}</span><span style="color: #DD0000">,&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<div class="example-contents"><p>以上例程会输出:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
Pook, cat, 3.2
Rickety Ride, goat, 9.7
Peaches, dog, 12.3
</pre></div>
</div>
</div>
<div class="example" id="example-1231">
<p><strong>Example #2 Calling stored procedures with IN and OUT parameters</strong></p>
<div class="example-contents"><p>
The stored procedure match_animal in the following example accepts
three different parameters:
<ol type="1">
<li class="listitem">
<p class="para">
an input (IN) parameter that accepts the name of the first animal as
input
</p>
</li>
<li class="listitem">
<p class="para">
an input-output (INOUT) parameter that accepts the name of the second
animal as input and returns the string <em>TRUE</em> if an
animal in the database matches that name
</p>
</li>
<li class="listitem">
<p class="para">
an output (OUT) parameter that returns the sum of the weight of the
two identified animals
</p>
</li>
</ol>
In addition, the stored procedure returns a result set consisting of the
animals listed in alphabetic order starting at the animal corresponding
to the input value of the first parameter and ending at the animal
corresponding to the input value of the second parameter.
</p></div>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$sql&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'CALL&nbsp;match_animal(?,&nbsp;?,&nbsp;?)'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$database</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Peaches"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$second_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Rickety&nbsp;Ride"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$weight&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">db2_bind_param</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"name"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">DB2_PARAM_IN</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">db2_bind_param</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"second_name"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">DB2_PARAM_INOUT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">db2_bind_param</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"weight"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">DB2_PARAM_OUT</span><span style="color: #007700">);<br /><br />print&nbsp;</span><span style="color: #DD0000">"Values&nbsp;of&nbsp;bound&nbsp;parameters&nbsp;_before_&nbsp;CALL:\n"</span><span style="color: #007700">;<br />print&nbsp;</span><span style="color: #DD0000">"&nbsp;&nbsp;1:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$name</span><span style="color: #007700">}</span><span style="color: #DD0000">&nbsp;2:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$second_name</span><span style="color: #007700">}</span><span style="color: #DD0000">&nbsp;3:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$weight</span><span style="color: #007700">}</span><span style="color: #DD0000">\n\n"</span><span style="color: #007700">;<br /><br />if&nbsp;(</span><span style="color: #0000BB">db2_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"Values&nbsp;of&nbsp;bound&nbsp;parameters&nbsp;_after_&nbsp;CALL:\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"&nbsp;&nbsp;1:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$name</span><span style="color: #007700">}</span><span style="color: #DD0000">&nbsp;2:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$second_name</span><span style="color: #007700">}</span><span style="color: #DD0000">&nbsp;3:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$weight</span><span style="color: #007700">}</span><span style="color: #DD0000">\n\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"Results:\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"&nbsp;&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]}</span><span style="color: #DD0000">,&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]}</span><span style="color: #DD0000">,&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<div class="example-contents"><p>以上例程会输出:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
Values of bound parameters _before_ CALL:
1: Peaches 2: Rickety Ride 3: 0
Values of bound parameters _after_ CALL:
1: Peaches 2: TRUE 3: 22
Results:
Peaches, dog, 12.3
Pook, cat, 3.2
Rickety Ride, goat, 9.7
</pre></div>
</div>
</div>
<div class="example" id="example-1232">
<p><strong>Example #3 Inserting a binary large object (BLOB) directly from a file</strong></p>
<div class="example-contents"><p>
The data for large objects are typically stored in files, such as XML
documents or audio files. Rather than reading an entire file into a PHP
variable, and then binding that PHP variable into an SQL statement, you
can avoid some memory overhead by binding the file directly to the input
parameter of your SQL statement. The following example demonstrates how
to bind a file directly into a BLOB column.
</p></div>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;animal_pictures(picture)&nbsp;VALUES&nbsp;(?)"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$picture&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"/opt/albums/spook/grooming.jpg"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$rc&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_bind_param</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"picture"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">DB2_PARAM_FILE</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rc&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
</div>
</p>
</div>
<div class="refsect1 seealso" id="refsect1-function.db2-bind-param-seealso">
<h3 class="title">参见</h3>
<p class="para">
<ul class="simplelist">
<li class="member"><span class="function"><a href="db2_execute.html" class="function" rel="rdfs-seeAlso">db2_execute()</a> - Executes a prepared SQL statement</span></li>
<li class="member"><span class="function"><a href="db2_prepare.html" class="function" rel="rdfs-seeAlso">db2_prepare()</a> - Prepares an SQL statement to be executed</span></li>
</ul>
</p>
</div>
</div></div></div></body></html>