How to Get The Count or The Number Of Rows In A Result Set In PHP using ODBC Connection?

前端 未结 3 350
青春惊慌失措
青春惊慌失措 2021-01-14 01:53

While I build a web page in My PHP web application, My Connection works ok but When I want to get the count of rows of the SELECT Statement I used in my query, It gives me -

相关标签:
3条回答
  • 2021-01-14 02:51

    odbc_num_rows seems to be reliable for INSERT, UPDATE, and DELETE queries only.

    The manual says:

    Using odbc_num_rows() to determine the number of rows available after a SELECT will return -1 with many drivers.

    one way around this behaviour is to do a COUNT(*) in SQL instead. See here for an example.

    0 讨论(0)
  • 2021-01-14 02:53

    in php.net:

    The easy way to count the rows in an odbc resultset where the driver returns -1 is to let SQL do the work:

    <?php
    
        $conn = odbc_connect("dsn", "", "");
        $rs = odbc_exec($conn, "SELECT Count(*) AS counter FROM tablename WHERE fieldname='" . $value . "'");
        $arr = odbc_fetch_array($rs);
        echo $arr['counter'];
    
    ?>
    
    0 讨论(0)
  • 2021-01-14 02:53

    On what basis, do you expect odbc_num_rows to return anything other than -1 ?

    We have the fact from the manuals, that OBDC does not support @@ROWCOUNT / odbc_num_rows. So there is no basis for expecting that it "should" return anything other than that which is documented, -1 in all circumstances.

    Even if you used Sybase directly (instead of via ODBC), you would have the same "problem".

    • odbc_num_rows returns @@ROWCOUNT, which is the rows inserted/updated/deleted by the immediately preceding command. -1 is the correct, documented value, if the immediately preceding command is not insert/update/delete.

    • It has nothing to do with rows in a table.

    Use another batch, and either one of the documented methods to obtain rows in a table, and load the value into a variable:

    • SELECT @Count = COUNT(*) -- slow, requires I/O
      or
    • SELECT @Count = ROW_COUNT(db_id, object_id) -- very fast, no I/O

    Then interrogate the result array, to obtain the variable, not odbc_num_rows, which will continue returning -1.

    0 讨论(0)
提交回复
热议问题