📄 function.db2-bind-param.html
字号:
<dd> <p class="para"> Specifies the scale with which the variable should be bound to the database. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <b><tt>TRUE</tt></b> on success or <b><tt>FALSE</tt></b> on failure. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 Binding PHP variables to a prepared statement</b></p> <div class="example-contents"><p> The SQL statement in the following example uses two input parameters in the WHERE clause. We call <b>db2_bind_param()</b> 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 <b>db2_bind_param()</b>; in the example, <i>$lower_limit</i> is assigned a value before the call to <b>db2_bind_param()</b>, but <i>$upper_limit</i> is assigned a value after the call to <b>db2_bind_param()</b>. The variables must be bound and, for parameters that accept input, must have any value assigned, before calling <a href="function.db2-execute.html" class="function">db2_execute()</a>. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br />$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">'SELECT name, breed, weight FROM animals<br /> WHERE weight > ? AND weight < ?'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$database</span><span style="color: #007700">, </span><span style="color: #0000BB">$user</span><span style="color: #007700">, </span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// We can declare the variable before calling db2_bind_param()<br /></span><span style="color: #0000BB">$lower_limit </span><span style="color: #007700">= </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">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">"lower_limit"</span><span style="color: #007700">, </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">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">"upper_limit"</span><span style="color: #007700">, </span><span style="color: #0000BB">DB2_PARAM_IN</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// We can also declare the variable after calling db2_bind_param()<br /></span><span style="color: #0000BB">$upper_limit </span><span style="color: #007700">= </span><span style="color: #0000BB">15.0</span><span style="color: #007700">;<br /><br />if (</span><span style="color: #0000BB">db2_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">)) {<br /> while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">)) {<br /> print </span><span style="color: #DD0000">"{$row[0]}, {$row[1]}, {$row[2]}\n"</span><span style="color: #007700">;<br /> }<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>Pook, cat, 3.2Rickety Ride, goat, 9.7Peaches, dog, 12.3</pre></div> </pre></div> </div> <div class="example"> <p><b>Example #2 Calling stored procedures with IN and OUT parameters</b></p> <div class="example-contents"><p> The stored procedure match_animal in the following example accepts three different parameters: <ol class="orderedlist"> <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 <i>TRUE</i> 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"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br />$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">'CALL match_animal(?, ?, ?)'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$database</span><span style="color: #007700">, </span><span style="color: #0000BB">$user</span><span style="color: #007700">, </span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #DD0000">"Peaches"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$second_name </span><span style="color: #007700">= </span><span style="color: #DD0000">"Rickety Ride"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$weight </span><span style="color: #007700">= </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">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">"name"</span><span style="color: #007700">, </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">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">"second_name"</span><span style="color: #007700">, </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">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #DD0000">"weight"</span><span style="color: #007700">, </span><span style="color: #0000BB">DB2_PARAM_OUT</span><span style="color: #007700">);<br /><br />print </span><span style="color: #DD0000">"Values of bound parameters _before_ CALL:\n"</span><span style="color: #007700">;<br />print </span><span style="color: #DD0000">" 1: {$name} 2: {$second_name} 3: {$weight}\n\n"</span><span style="color: #007700">;<br /><br />if (</span><span style="color: #0000BB">db2_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">)) {<br /> print </span><span style="color: #DD0000">"Values of bound parameters _after_ CALL:\n"</span><span style="color: #007700">;<br /> print </span><span style="color: #DD0000">" 1: {$name} 2: {$second_name} 3: {$weight}\n\n"</span><span style="color: #007700">;<br /><br /> print </span><span style="color: #DD0000">"Results:\n"</span><span style="color: #007700">;<br /> while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">)) {<br /> print </span><span style="color: #DD0000">" {$row[0]}, {$row[1]}, {$row[2]}\n"</span><span style="color: #007700">;<br /> }<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>Values of bound parameters _before_ CALL: 1: Peaches 2: Rickety Ride 3: 0Values of bound parameters _after_ CALL: 1: Peaches 2: TRUE 3: 22Results: Peaches, dog, 12.3 Pook, cat, 3.2 Rickety Ride, goat, 9.7</pre></div> </pre></div> </div> <div class="example"> <p><b>Example #3 Inserting a binary large object (BLOB) directly from a file</b></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"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">"INSERT INTO animal_pictures(picture) VALUES (?)"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$picture </span><span style="color: #007700">= </span><span style="color: #DD0000">"/opt/albums/spook/grooming.jpg"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$rc </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_bind_param</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">"picture"</span><span style="color: #007700">, </span><span style="color: #0000BB">DB2_PARAM_FILE</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rc </span><span style="color: #007700">= </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">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.db2-execute.html" class="function" rel="rdfs-seeAlso">db2_execute()</a></li> <li class="member"><a href="function.db2-prepare.html" class="function" rel="rdfs-seeAlso">db2_prepare()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.db2-autocommit.html">db2_autocommit</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.db2-client-info.html">db2_client_info</a></div> <div class="up"><a href="ref.ibm-db2.html">IBM DB2 Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -