Faqts : Computers : Programming : Shopping For You : PHP : Database Backed Sites : ODBC

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

48 of 58 people (83%) answered Yes
Recently 7 of 10 people (70%) answered Yes


Why is odbc_num_rows always returning -1?
How can I work around the problem where odbc_num_rows always returns -1?

Sep 29th, 2000 11:48
Derek Harding, Sylvester Burckhardt, Nathan Wallace, Keith Greene

If you check out
you will find a note that reads:
    Note: Using odbc_num_rows() to determine the number of rows 
          available after a SELECT will return -1 with many drivers.
This unfortunately happens to be the case with Access, and MS SQL I
The problem doesn't seem to be with Access/MS SQL. When using iodbc &
openlink I was receiving a correct number of rows. However, when I
switched to unixODBC & Easysoft's OOB I found I was receiving only -1. I
haven't yet tracked down if it is the driver manager or those specific
drivers but clearly it isn't the databases themselves.
The workaround I use is this:
    $connect = odbc_connect("your_db", "user", "pass");
    $result = odbc_exec($connect,"select * from your_table");
    while($temp = odbc_fetch_into($result,&$counter)){
$count will contain the number of rows for that particular select
statement. You can also use ODBC_RESULT in place of the ODBC_FETCH_INTO.
I can offer another workaround:
Apply the count function from SQL to your query and read the result back 
like the result of a normal query.
Here an example:
if ($od = odbc_connect("Cadisdat_DB","","")) {
  $sql = "SELECT Count(*) FROM Fahrzeuge";
  if ($or = odbc_exec($od, $sql)) {
    $flg = odbc_fetch_row($or);
    $cnt = odbc_result($or,1);
    echo "Count = " . $cnt;
This costs also two queries on the same data, but you don't have to read 
in every line.