⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function.db2-bind-param.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 2 页
字号:
     <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">&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">"{$row[0]},&nbsp;{$row[1]},&nbsp;{$row[2]}\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</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">&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;{$name}&nbsp;2:&nbsp;{$second_name}&nbsp;3:&nbsp;{$weight}\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;{$name}&nbsp;2:&nbsp;{$second_name}&nbsp;3:&nbsp;{$weight}\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;{$row[0]},&nbsp;{$row[1]},&nbsp;{$row[2]}\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</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">&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></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 + -