PHP: oci_num_fields - Manual (original) (raw)

(PHP 5, PHP 7, PHP 8, PECL OCI8 >= 1.1.0)

oci_num_fields β€” Returns the number of result columns in a statement

Description

Parameters

statement

A valid OCI statement identifier.

Return Values

Returns the number of columns as an int.

Examples

Example #1 oci_num_fields() example

<?php// Create the table with: // CREATE TABLE mytab (id NUMBER, quantity NUMBER);$conn = oci_connect("hr", "hrpwd", "localhost/XE"); if (!$conn) { $m = oci_error(); trigger_error(htmlentities($m['message']), E_USER_ERROR); }$stid = oci_parse($conn, "SELECT * FROM mytab"); oci_execute($stid, OCI_DESCRIBE_ONLY); // Use OCI_DESCRIBE_ONLY if not fetching rows$ncols = oci_num_fields($stid); for ($i = 1; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mo>&lt;</mo><mo>=</mo></mrow><annotation encoding="application/x-tex">i &lt;= </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6986em;vertical-align:-0.0391em;"></span><span class="mord mathnormal">i</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&lt;=</span></span></span></span>ncols; $i++) { echo oci_field_name($stid, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mo stretchy="false">)</mo><mi mathvariant="normal">.</mi><mi mathvariant="normal">&quot;</mi><mi mathvariant="normal">&quot;</mi><mi mathvariant="normal">.</mi><mi>o</mi><mi>c</mi><msub><mi>i</mi><mi>f</mi></msub><mi>i</mi><mi>e</mi><mi>l</mi><msub><mi>d</mi><mi>t</mi></msub><mi>y</mi><mi>p</mi><mi>e</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">i) . &quot; &quot; . oci_field_type(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.0361em;vertical-align:-0.2861em;"></span><span class="mord mathnormal">i</span><span class="mclose">)</span><span class="mord">.&quot;&quot;.</span><span class="mord mathnormal">oc</span><span class="mord"><span class="mord mathnormal">i</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.10764em;">f</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord mathnormal">i</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">t</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mord mathnormal">p</span><span class="mord mathnormal">e</span><span class="mopen">(</span></span></span></span>stid, $i) . "<br>\n"; }// Outputs: // ID NUMBER // QUANTITY NUMBERoci_free_statement($stid); oci_close($conn);?>

Found A Problem?

jnield at impole dot com ΒΆ

26 years ago

The following is not immediately obvious:

If you need the number of columns in a REF CURSOR returned from a PL/SQL procedure, you need to use OCINumColumns() on the cursor handle returned by OCINewCursor after it is bound and executed, not the statement handle. Same applies for OCIColumnName() and friends.