PHP: Hypertext Preprocessor (original) (raw)

oci_num_rows

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

oci_num_rows — Returns number of rows affected during statement execution

Description

Parameters

statement

A valid OCI statement identifier.

Return Values

Returns the number of rows affected as an integer, or [false](reserved.constants.php#constant.false) on failure

Examples

Example #1 oci_num_rows() example

`<?php

$conn

= oci_connect("hr", "hrpwd", "localhost/XE");
if (!$conn) {
$m = oci_error();
trigger_error(htmlentities($m['message']), E_USER_ERROR);
}$stid = oci_parse($conn, "create table emp2 as select * from employees");
oci_execute($stid);
echo oci_num_rows($stid) . " rows inserted.
\n";
oci_free_statement($stid);$stid = oci_parse($conn, "delete from emp2");
oci_execute($stid, OCI_DEFAULT);
echo oci_num_rows($stid) . " rows deleted.
\n";
oci_commit($conn);
oci_free_statement($stid);$stid = oci_parse($conn, "drop table emp2");
oci_execute($stid);
oci_free_statement($stid);oci_close($conn);?>`

Notes

Note:

This function does not return number of rows selected! For SELECT statements this function will return the number of rows, that were fetched to the buffer with oci_fetch*() functions.

Found A Problem?

vihanga dot kule99 at gmail dot com

1 year ago

`` The oci_num_rows() function is used to retrieve the number of rows affected or returned by a query executed with Oracle Database using the OCI8 extension. Here's an explanation of how oci_num_rows() works:

  1. Syntax:
    php
    oci_num_rows($statement);

  2. Parameters:
    $statement: This parameter represents the Oracle statement handle returned by oci_parse() and executed using oci_execute(). It refers to the executed query or statement for which you want to get the number of rows.

  3. Return Value:
    The oci_num_rows() function returns the number of rows affected or returned by the executed query. It returns an integer value representing the count of rows.

  4. Usage:
    After executing a query with oci_execute(), you can use oci_num_rows() to determine the number of rows affected or returned by the query.

example that demonstrates the usage of oci_num_rows():

php sql="SELECT∗FROMemployees";<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>e</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mo>=</mo><mi>o</mi><mi>c</mi><msub><mi>i</mi><mi>p</mi></msub><mi>a</mi><mi>r</mi><mi>s</mi><mi>e</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">statement=ociparse(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6151em;"></span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">m</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1.0361em;vertical−align:−0.2861em;"></span><spanclass="mordmathnormal">oc</span><spanclass="mord"><spanclass="mordmathnormal">i</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:−2.55em;margin−left:0em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight">p</span></span></span></span><spanclass="vlist−s">​</span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.2861em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal">rse</span><spanclass="mopen">(</span></span></span></span>connection,sql = "SELECT * FROM employees"; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>e</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mo>=</mo><mi>o</mi><mi>c</mi><msub><mi>i</mi><mi>p</mi></msub><mi>a</mi><mi>r</mi><mi>s</mi><mi>e</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">statement = oci_parse(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6151em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0361em;vertical-align:-0.2861em;"></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.1514em;"><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">p</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">a</span><span class="mord mathnormal">rse</span><span class="mopen">(</span></span></span></span>connection, sql="SELECTFROMemployees";<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>e</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mo>=</mo><mi>o</mi><mi>c</mi><msub><mi>i</mi><mi>p</mi></msub><mi>a</mi><mi>r</mi><mi>s</mi><mi>e</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">statement=ociparse(</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6151em;"></span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">m</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1.0361em;verticalalign:0.2861em;"></span><spanclass="mordmathnormal">oc</span><spanclass="mord"><spanclass="mordmathnormal">i</span><spanclass="msupsub"><spanclass="vlisttvlistt2"><spanclass="vlistr"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:2.55em;marginleft:0em;marginright:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingresetsize6size3mtight"><spanclass="mordmathnormalmtight">p</span></span></span></span><spanclass="vlists"></span></span><spanclass="vlistr"><spanclass="vlist"style="height:0.2861em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal">rse</span><spanclass="mopen">(</span></span></span></span>connection,sql);
oci_execute($statement);

// Get the number of rows returned by the query numRows=ocinumrows(numRows = oci_num_rows(numRows=ocinumrows(statement);
echo "Number of rows: " . $numRows;

this example, we execute a SELECT query to fetch records from the "employees" table. After executing the query with oci_execute(), we use oci_num_rows() to retrieve the number of rows returned by the query and store it in the $numRows variable. Finally, we echo the count of rows.

``

pluueer at hotmail dot com

15 years ago

`If you want to return te number of rows without fetching all data it might by more efficient to use this code (correct me if I'm wrong): sqlquery=′SELECTCOUNT(∗)ASNUMBEROFROWSFROM(′.sql_query = 'SELECT COUNT(*) AS NUMBER_OF_ROWS FROM (' . sqlquery=SELECTCOUNT()ASNUMBEROFROWSFROM(.your_query . ')'; stmt=ociparse(stmt= oci_parse(stmt=ociparse(conn, $sql_query);

oci_define_by_name($stmt, 'NUMBER_OF_ROWS', $number_of_rows);

oci_execute($stmt);

oci_fetch($stmt);

echo $number_of_rows;

`