php num_rowr,odbc_num_rows

用户评论:

[#1]

panchome66 at gmail dot com [2014-02-09 21:22:56]

Este codigo se prob?? en SQL Server 2000, no se prob?? en otras versiones como 2005 o 2008. Aun asi no se aplica para MySQL, porque no existe la tabla "sysindexes":

$cnx = odbc_connect("dbSQLEmpresa","Admin","123");

if ($cnx)

{

$rs = odbc_exec($cnx, "SELECT * FROM alumnos");

$f = odbc_num_fields($rs);

$r = odbc_num_rows($rs);

$r = LFRJ_odbc_num_rows($cnx,"alumnos");

echo "

for ($i = 1; $i <= $f; $i++)

{

$n = odbc_field_name($rs, $i);

echo "

", $n, "";

}

while(odbc_fetch_row($rs))

{

echo "

";

for ($i = 1; $i <= $f; $i++)

{

$d = odbc_result($rs, $i);

echo "

", $d, "";

}

echo "

";

}

echo "

Campos(" . $f . ") Registros(" . $r . ")";

echo "

";

}

odbc_close($cnx);

function LFRJ_odbc_num_rows($cnx,$Tabla)

{

$rs = odbc_exec($cnx, "SELECT rows FROM sysindexes WHERE id = OBJECT_ID('" . $Tabla . "') AND indid 

return odbc_result($rs, 1);

}

[#2]

johnnyboyct-AT-yahoo.com [2013-06-19 17:35:48]

function best_odbc_num_rows($r1)  {

ob_start(); // block printing table with results

(int)$number=odbc_result_all($r1);

ob_clean(); // block printing table with results

return $number;

}

Above is the best way to count if you are not using something like IBM Netezza and ODBC and not doing more than 100000ish records, otherwise even this method will run out of memory.

IBM Netezza and ODBC will give you counts matching the prefetch setting in the odbc.ini file :( the default is 256 so watch out because it is accurate until that number.

[#3]

Gerd Christian Kunze [2013-01-23 12:34:37]

odbc_num_rows does return -1 when it shouldn't.

i used this code:

while(false!== ($Row= @odbc_fetch_array($Result) ) ) {// do something with $Row}

}

else {

returnfalse;

}?>

and it didn't work... obviously

but this while loop will skip an empty result set anyway, so i use this:

if( !odbc_num_rows($Result) ) {

returnfalse;

}?>

because after processing the $Result with fetch, odbc_num_rows reports the correct count (false|0..n) ... magic :-)

[#4]

pjavilla at gmail dot com [2012-10-01 04:07:30]

When accessing a DB2 database with PHP via the ODBC functions, beware of statements which include references to IBM's LONGDESCRIPTION table (for example, if you were - like myself - digging through IBM's Maximo product). Reading from that table usually requires error suppression, because although it works ODBC will spit out a warning message onscreen.

When you make it part of another query however, ODBC_NUM_ROWS will always return -1. The solution is to keep any queries to LONGDESCRIPTION in a separate standalone query by itself.

However, if you run the query though a previewer like Toad for DB2, using LONGDESCRIPTION in a larger query is fine and does show the results. It's just that you have to break the query up and segregate the query to LONGDESCRIPTION on its own if you are writing ODBC queries for PHP.

Just a quick note for anyone else who found ODBC_NUM_RESULTS normally reliable otherwise but inexplicably always returning -1 under certain circumstances.

[#5]

walt at brookhouse dot co dot uk [2010-07-09 14:45:05]

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

$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'];?>

[#6]

chew_baka at hotmail dot com [2010-05-18 14:44:35]

None of these examples were working for me, so I came up with the following silly procedure that gives me the number of rows.  This example is crude, but you should get the idea.

$cxn=odbc_connect("ODBC_DSN_NAME","","");$sql="SELECT * FROM some_table'";$res=odbc_exec($cxn,$sql);$items=0;

while ($row=odbc_fetch_array($res))

{$items++;

}odbc_free_result($res);

echo"
total No. of rows:$items";?>

[#7]

jeff at script-xs dot com [2009-01-02 11:34:21]

After minutes of frustration, I realized why odbc_num_rows was not returning the number of affected rows on a prepared update query.  I'm using ODBC to connect to Microsot SQL Server 2005.

My corrected code:

$query=odbc_prepare($conn,'UPDATE table SET cat = ? WHERE id = 1');$result=odbc_execute($query,$category);$affected=odbc_num_rows($query);?>

This code works.  I was frustrated that odbc_num_rows($result) didn't work as I expected, but instead required me to pass the original prepared query to this function.

[#8]

sirio3mil at gmail dot com [2008-06-17 09:46:50]

The diference between functions used here are consierable, for example for one table with 36 columns and 806 rows the time to execute two of those functions are this:

function using odbc odbc_result_all take 2,6 seconds

function using odbc_fetch_row take 0,8 seconds

[#9]

areznik at survdata dot com [2007-11-30 10:04:21]

I could have been noted before in this thread but I couldnt find it on my first search.

This function (odbc_num_rows) returns -1 when ODBCing to MS SQL and making it hard to get the number of rows in the returned recordset.

Two workarounds:

1. When you just need to verify that any rows returned from your query you can use select count(*) as cnt from table and then just get $row['cnt']

2. When you need to actually loop through the records this function returns number of rows in the recordset if and only if you include ORDER BY clause in your query statement.

That sounds a bit annoying but thats the work around when dealing with MS SQL odbc driver I guess.

It would be beneficial if someone explained how the Order By clause makes the difference.

[#10]

Nathaniel at comtel dot com dot au [2007-10-30 20:57:10]

My development computer is running XP sql2005 while the production copy sits on a server 2003R2 sql2000 computer.

In the course of trying to get this function to work (switching from mssql to odbc) I have discovered that the ODBC driver versions are different between the two OS and that while the newer version (release date 17/2/07) that is able to be installed on 2003 handles this function fine, the older version doesn't.

Microsoft sites suggest that Vista might also handle it (ie have the newer driver). It also says that there are no plans to release the newer driver in a installable package.

http://support.microsoft.com/kb/892854

Will hopefully test with the sql2005 on server 2003R2 in the near future to confirm it is the driver helping here.

[#11]

pmo@raadvst-consetatDOTbe [2007-10-16 02:46:08]

voland's function is simply great.

However, i would recommend the use of ob_end_clean(), to shut down completely the output buffer (can cause weird behaviour).

[#12]

nielsvandenberge at hotm dot dot dot dot dot com [2007-09-11 05:06:24]

I just tried to use the function best_odbc_num_rows($result) from voland at digitalshop dot ru, but it's not working quite well. After executing the function odbc_result_all(); the resultset has to be resetted again.

Resetting it with

odbc_fetch_row($result, 0);

is not working for me.

I think the internal number-value of the odbc_result_all()-function is not resetted, but that's just a guess.

when I execute the function 3 times with a resultset of 17 rows the values: 17, 34 and 51 are returned.

His previous function useful_odbc_num_rows($result) works better (for me).

[#13]

voland at digitalshop dot ru [2007-03-26 07:50:53]

Today we find a BEST way to count number of rows with ODBC!

function best_odbc_num_rows($r1)  {

ob_start(); // block printing table with results

(int)$number=odbc_result_all($r1);

ob_clean(); // block printing table with results

return $number;

}

[#14]

voland at digitalshop dot ru [2007-03-05 05:56:32]

After a hour for a searching a good alter function of odbc_num_rows... i try to write it by mysels:

function useful_odbc_num_rows($result){

$num_rows=0;

while($temp = odbc_fetch_into($result, &$counter))

{

$num_rows++;

}

@odbc_fetch_row($result, 0);   // reset cursor

return $num_rows;

}

[#15]

dm at personalcomputingsolutions dot co dot uk [2007-02-07 13:41:37]

function db_get_row($cur, $rownum){

if (odbc_fetch_into($cur, $row, $rownum)){

return ($row);

}else{

return (FALSE);

}

$i=1;

if (db_get_row($cur,1)){

while ($record=db_get_row($cur,$i++)){

do stuff

}else{

tell the user there are no results

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值