Most efficient alternative to odbc_num_rows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Purple
    Recognized Expert Contributor
    • May 2007
    • 404

    Most efficient alternative to odbc_num_rows

    Hi All,

    I am running PHP5.3 with MSSQL 2000..

    obdc_num_rows is returning error on select statements due to the odbc driver (the value is only valid when modifying rows !?!).

    My question is - given I have a result set, what is the most efficient way to count the rows held within.

    Regards and thanks in advance,

    Purple
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Purple.

    count() will probably be your friend. Once you load your resultset into a PHP array, you can count() the number of rows.

    Or you could use key(end($array) ) + 1 if you're using numeric indexes for your rows, starting with 0.

    Comment

    • Purple
      Recognized Expert Contributor
      • May 2007
      • 404

      #3
      Pbmods,

      thanks as ever for your response, I am using count() currently to work around this issue with odbc_fetch_arra y.

      I am looking at some AJAX datagrid controls and moving data around using odbc_fetch_obje ct and json - obviously I can count the elements in the array fed into json_encode..

      Is count() the fastest way to do it ? Would running a counter in the odbc_fetch loop be quicker ? Should I care :)

      Regards Purple

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Purple.

        I would think that you could do this, and it should be slightly faster than using count():

        [code=php]
        $numResults = key(end($result s)) + 1;
        [/code]

        For example, if your results looked like this:
        [code=php]
        $results = array(
        0 => array(
        // ... row data ...
        ),
        1 => array(
        // ... row data ...
        ),
        .
        .
        .
        18 => array(
        // ... row data ...
        )
        );
        [/code]

        Then end($results) would set the internal pointer for $results to its last element, then key() would return '18', but since there are actually 19 rows, we have to add 1.

        The other option is to add a counter to your DAL.

        Comment

        Working...