<!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>Sets the result set pointer to the next row or requested row</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.db2-fetch-object.html">db2_fetch_object</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.db2-field-display-size.html">db2_field_display_size</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><hr /><div id="function.db2-fetch-row" class="refentry"> <div class="refnamediv"> <h1 class="refname">db2_fetch_row</h1> <p class="verinfo">(PECL ibm_db2 >= 1.0.0)</p><p class="refpurpose"><span class="refname">db2_fetch_row</span> — <span class="dc-title"> Sets the result set pointer to the next row or requested row </span></p> </div> <div class="refsect1 description" id="refsect1-function.db2-fetch-row-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>db2_fetch_row</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">$row_number</code></span> ] )</div> <p class="para rdfs-comment"> Use <span class="function"><strong>db2_fetch_row()</strong></span> to iterate through a result set, or to point to a specific row in a result set if you requested a scrollable cursor. </p> <p class="para"> To retrieve individual fields from the result set, call the <span class="function"><a href="function.db2-result.html" class="function">db2_result()</a></span> function. </p> <p class="para"> Rather than calling <span class="function"><strong>db2_fetch_row()</strong></span> and <span class="function"><a href="function.db2-result.html" class="function">db2_result()</a></span>, most applications will call one of <span class="function"><a href="function.db2-fetch-assoc.html" class="function">db2_fetch_assoc()</a></span>, <span class="function"><a href="function.db2-fetch-both.html" class="function">db2_fetch_both()</a></span>, or <span class="function"><a href="function.db2-fetch-array.html" class="function">db2_fetch_array()</a></span> to advance the result set pointer and return a complete row as an array. </p> </div> <div class="refsect1 parameters" id="refsect1-function.db2-fetch-row-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">stmt</code></em></span> <dd> <p class="para"> A valid <em>stmt</em> resource. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">row_number</code></em></span> <dd> <p class="para"> With scrollable cursors, you can request a specific row number in the result set. Row numbering is 1-indexed. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.db2-fetch-row-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> if the requested row exists in the result set. Returns <strong><code>FALSE</code></strong> if the requested row does not exist in the result set. </p> </div> <div class="refsect1 examples" id="refsect1-function.db2-fetch-row-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-1170"> <p><strong>Example #1 Iterating through a result set</strong></p> <div class="example-contents"><p> The following example demonstrates how to iterate through a result set with <span class="function"><strong>db2_fetch_row()</strong></span> and retrieve columns from the result set with <span class="function"><a href="function.db2-result.html" class="function">db2_result()</a></span>. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">'SELECT name, breed FROM animals WHERE weight < ?'</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 /></span><span style="color: #0000BB">db2_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">, array(</span><span style="color: #0000BB">10</span><span style="color: #007700">));<br />while (</span><span style="color: #0000BB">db2_fetch_row</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$breed </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_result</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">);<br /> print </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$name</span><span style="color: #DD0000"> </span><span style="color: #0000BB">$breed</span><span style="color: #DD0000">"</span><span style="color: #007700">;<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 screen"> <div class="cdata"><pre> cat Pook gold fish Bubbles budgerigar Gizmo goat Rickety Ride </pre></div> </div> </div> <div class="example" id="example-1171"> <p><strong>Example #2 i5/OS recommended alternatives to db2_fetch_row/db2_result</strong></p> <div class="example-contents"><p> On i5/OS it is recommended that you use <span class="function"><a href="function.db2-fetch-both.html" class="function">db2_fetch_both()</a></span>, <span class="function"><a href="function.db2-fetch-array.html" class="function">db2_fetch_array()</a></span>, or <span class="function"><a href="function.db2-fetch-object.html" class="function">db2_fetch_object()</a></span> over <span class="function"><strong>db2_fetch_row()</strong></span>/ <span class="function"><a href="function.db2-result.html" class="function">db2_result()</a></span>. In general <span class="function"><strong>db2_fetch_row()</strong></span>/ <span class="function"><a href="function.db2-result.html" class="function">db2_result()</a></span> have more issues with various column types in <em>EBCIDIC</em> to <em>ASCII</em> translation, including possible truncation in <em>DBCS</em> applications. You may also find the performance of <span class="function"><a href="function.db2-fetch-both.html" class="function">db2_fetch_both()</a></span>, <span class="function"><a href="function.db2-fetch-array.html" class="function">db2_fetch_array()</a></span>, and <span class="function"><a href="function.db2-fetch-object.html" class="function">db2_fetch_object()</a></span> to be superior to <span class="function"><strong>db2_fetch_row()</strong></span>/ <span class="function"><a href="function.db2-result.html" class="function">db2_result()</a></span>. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /> $conn </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">""</span><span style="color: #007700">,</span><span style="color: #DD0000">""</span><span style="color: #007700">,</span><span style="color: #DD0000">""</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">'SELECT SPECIFIC_SCHEMA, SPECIFIC_NAME, ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE, ROUTINE_CREATED, ROUTINE_BODY, IN_PARMS, OUT_PARMS, INOUT_PARMS, PARAMETER_STYLE, EXTERNAL_NAME, EXTERNAL_LANGUAGE FROM QSYS2.SYSROUTINES FETCH FIRST 2 ROWS ONLY'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_exec</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">, array(</span><span style="color: #DD0000">'cursor' </span><span style="color: #007700">=> </span><span style="color: #0000BB">DB2_SCROLLABLE</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_both</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">)){<br /> echo </span><span style="color: #DD0000">"<br>db2_fetch_both </span><span style="color: #007700">{</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'SPECIFIC_NAME'</span><span style="color: #007700">]}</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: #DD0000">'ROUTINE_CREATED'</span><span style="color: #007700">]}</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">5</span><span style="color: #007700">]}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /> }<br /> </span><span style="color: #0000BB">$stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_exec</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">, array(</span><span style="color: #DD0000">'cursor' </span><span style="color: #007700">=> </span><span style="color: #0000BB">DB2_SCROLLABLE</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 /> echo </span><span style="color: #DD0000">"<br>db2_fetch_array </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"> </span><span style="color: #007700">{</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">5</span><span style="color: #007700">]}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /> }<br /> </span><span style="color: #0000BB">$stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_exec</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">, array(</span><span style="color: #DD0000">'cursor' </span><span style="color: #007700">=> </span><span style="color: #0000BB">DB2_SCROLLABLE</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_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">)){<br /> echo </span><span style="color: #DD0000">"<br>db2_fetch_object </span><span style="color: #007700">{</span><span style="color: #0000BB">$row</span><span style="color: #007700">-></span><span style="color: #0000BB">SPECIFIC_NAME</span><span style="color: #007700">}</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">ROUTINE_CREATED</span><span style="color: #007700">}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /> }<br /> </span><span style="color: #0000BB">db2_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<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 screen"> <div class="cdata"><pre> db2_fetch_both MATCH_ANIMAL 2006-08-25-17.10.23.775000 2006-08-25-17.10.23.775000 db2_fetch_both MULTIRESULTS 2006-10-17-10.11.05.308000 2006-10-17-10.11.05.308000 db2_fetch_array MATCH_ANIMAL 2006-08-25-17.10.23.775000 db2_fetch_array MULTIRESULTS 2006-10-17-10.11.05.308000 db2_fetch_object MATCH_ANIMAL 2006-08-25-17.10.23.775000 db2_fetch_object MULTIRESULTS 2006-10-17-10.11.05.308000 </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.db2-fetch-row-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.db2-fetch-array.html" class="function" rel="rdfs-seeAlso">db2_fetch_array()</a> - Returns an array, indexed by column position, representing a row in a result set</span></li> <li class="member"> <span class="function"><a href="function.db2-fetch-assoc.html" class="function" rel="rdfs-seeAlso">db2_fetch_assoc()</a> - Returns an array, indexed by column name, representing a row in a result set</span></li> <li class="member"> <span class="function"><a href="function.db2-fetch-both.html" class="function" rel="rdfs-seeAlso">db2_fetch_both()</a> - Returns an array, indexed by both column name and position, representing a row in a result set</span></li> <li class="member"> <span class="function"><a href="function.db2-fetch-object.html" class="function" rel="rdfs-seeAlso">db2_fetch_object()</a> - Returns an object with properties representing columns in the fetched row</span></li> <li class="member"> <span class="function"><a href="function.db2-result.html" class="function" rel="rdfs-seeAlso">db2_result()</a> - Returns a single column from a row in the result set</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.db2-fetch-object.html">db2_fetch_object</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.db2-field-display-size.html">db2_field_display_size</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>