How to get number of rows in php oci for SELECT statement

一曲冷凌霜 提交于 2019-12-08 13:01:42

问题


I require help to get number of rows from select statement which contain where clause. I have tried various option. But it didn't work for me. I have shown my code snippet below.

I'm getting o/p "0 rows fetched". But when I executed select query on Oracle DB, then it returns 1 row.

Hence, please help me to get number of select statement in PHP through OCI.

Environment:

  • Windows, Php 7.3 and Oracle 12c

Here is the Oracle query

CREATE TABLE users 
(
    no            INTEGER NOT NULL,
    id            VARCHAR2(200) NOT NULL,
    fullname      VARCHAR2(200),
    email         VARCHAR2(200),
    givenname     VARCHAR2(200),
    first_login   VARCHAR2(200),
    last_login    VARCHAR2(200)
);

INSERT INTO USERS (no, id,fullname,email,givenname,first_login,last_login) 
VALUES (1,'Bharat','Bharat','Bharat@gmail.com','Bharat','2019-08-20 03:08:42','2019-08-20 03:08:42');

INSERT INTO USERS (no, id,fullname,email,givenname,first_login,last_login) 
VALUES (1,'bharat','bharat','bharat@gmail.com','bharat','2019-08-20 03:08:42','2019-08-20 03:08:42');

PHP:

    <?php
    # connect localhost oracle
    $conn = oci_connect('c##admin', 'ibm123', '127.0.0.1/XE');
    $res="array"; #array to staore oci_fetch_all o/p
    #thow error if unable to make connection
    if (!$conn) {
        $m = oci_error();
        trigger_error(htmlentities($m['message']), E_USER_ERROR);
    }
    # Parse oracle query
    $stid = oci_parse($conn, "select * from users where ID='Bharat'");
    # execute statement
    oci_execute($stid);
    # fetch all content
    $nrows = oci_fetch_all($stid, $res);
    echo "$nrows rows fetched<br>\n";
    # Dump content
    var_dump($res);
    $numrows = oci_fetch_array($stid, OCI_BOTH);
    echo oci_num_rows($stid) . " rows.<br />\n";
    print_r($numrows);
    oci_free_statement($stid);
    oci_close($conn);
?>

Here is the output. Expected output is 1 row fetched.

0 rows fetched

Please help me to fix this issue

来源:https://stackoverflow.com/questions/57755957/how-to-get-number-of-rows-in-php-oci-for-select-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!