note 64007 deleted from function.odbc-fetch-row by danbrown
| From: | danbrown@php.net | Date: | Fri, 25 Jun 2010 13:43:19 +0000 |
| Subject: | note 64007 deleted from function.odbc-fetch-row by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170423@lists.php.net to get a copy of this message | ||
Note Submitter: michael dot buergi at gmx dot net
----
Thanks for the binary search code, romanziak. However, it does not work correct in all cases. I
corrected your version and added some code to complete the custom num_rows() function:
<?php
// note: there should be no calls to odbc_fetch_* after odbc_exec* and
// prior to a call to this function. it would affect the results.
//
// for this reason, you should cache the output of odbc_num_rows()
// and of this function.
//
// original code was tested with php 5.1.2 on iis and mssql server 8.0
//
function num_rows(&$rid) {
// try to use odbc_num_rows
$num= odbc_num_rows($rid);
if ($num >= 0) {
return $num;
}
// optimisation for frequent cases
if (!@odbc_fetch_row($rid, 1)) {
@odbc_fetch_row($rid, 0); // reset cursor
return 0;
}
if (!@odbc_fetch_row($rid, 2)) {
@odbc_fetch_row($rid, 0); // reset cursor
return 1;
}
// use binary search for counting rows for the driver
// won't give us this info...
$lo= 2;
$hi= 8192000; // max number of rows that can be in the result + 1
while ($lo < ($hi - 1)) {
$mid= (int)(($hi + $lo) / 2);
if (@odbc_fetch_row($rid, $mid)) {
$lo= $mid;
} else {
$hi= $mid;
}
}
$num= $lo;
@odbc_fetch_row($rid, 0); // reset cursor
return $num;
}
?>