如何使用优势数据库PHP扩展获取行数?

问题描述 投票:0回答:3

如何使用优势数据库PHP扩展获得SELECT语句结果集的行数?

php advantage-database-server
3个回答
1
投票

最后我自己写了一个类似于mysql_num_rows的函数。

function my_num_rows($result) {
    ob_start(); // begin disable output from ads_result_all
    (int)$number = ads_result_all($result);
    ob_end_clean(); //close and clean the output buffer
    ads_fetch_row($r1, 0); // reset the result set pointer to the beginning
    if ($number >= 0){
         return $number;
    } else {
         return FALSE;
    }
}

它也可以被重写成使用ads_fetch_row来计算行数,但这更容易满足我的需求。对于大的结果集,使用ads_result_all可能会有较慢的性能。


0
投票

你将不得不在获取行的时候计算它们。 (你可以看到这个KB项目 070618-1888)或者你可以用COUNT()标量执行第二个查询(如果可能的话,建议排除order by)

下面是一个边走边算的例子。

$rStmt = ads_do ($rConn, "select id, name from table1");
$RowCount = 0;
while (ads_fetch_row($rStmt))
{
   $id = ads_result ($rStmt, "id");
   $name = ads_result($rStmt, "name");
   echo $id . "\t" . $name . "\n";
   $RowCount++;
}
echo "RowCount:" . $RowCount . "\n";

0
投票

据我所知,在第12版中,你有 ads_num_rows()这个函数。更多的用法请看官方文档。

© www.soinside.com 2019 - 2024. All rights reserved.