473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Failure of odbc_num_rows sometimes

I am trying this query here because I got no useful reply on
alt.php.sql. If you can recommend a better venue for this question, I'd
be glad to hear it.

When I have an Sql statement of the form:

SELECT LOC_ID,REPEAT('--',LEVELX)||NAMEX
-- or SELECT LOC_ID,REPEAT('-',LEVELX*2)||NAMEX
FROM is3.locations
WHERE entity_id=2
AND (inactive='N' OR inactive IS NULL)
AND leftx BETWEEN 2 AND 983
ORDER BY LEFTX

odbc_num_rows returns -1. When I change the sql statement to:

SELECT LOC_ID,CHAR(LEVELX)||' '||NAMEX
FROM is3.locations
WHERE entity_id=2
AND (inactive='N' OR inactive IS NULL)
AND leftx BETWEEN 2 AND 983
ORDER BY LEFTX

odbc_num_rows returns the correct result.

Both statements work as expected in the ADS development environment.

The rdms is db2 udb v8.1.9 linux

php is 4.4.0 with db2 compiled in
'./configure' '--prefix=/usr/local/php-4.4.0'
'--with-apxs2=/usr/local/apache2.0.54/bin/apxs'
'--with-ibm-db2=/db2home/db2inst1/sqllib'
'--with-openssl=/usr/local/ssl-0.9.8'
Nov 29 '05 #1
9 2259
Bob Stearns wrote:
I am trying this query here because I got no useful reply on
alt.php.sql. If you can recommend a better venue for this question, I'd
be glad to hear it.

When I have an Sql statement of the form:

SELECT LOC_ID,REPEAT('--',LEVELX)||NAMEX
-- or SELECT LOC_ID,REPEAT('-',LEVELX*2)||NAMEX
FROM is3.locations
WHERE entity_id=2
AND (inactive='N' OR inactive IS NULL)
AND leftx BETWEEN 2 AND 983
ORDER BY LEFTX

odbc_num_rows returns -1. When I change the sql statement to:

SELECT LOC_ID,CHAR(LEVELX)||' '||NAMEX
FROM is3.locations
WHERE entity_id=2
AND (inactive='N' OR inactive IS NULL)
AND leftx BETWEEN 2 AND 983
ORDER BY LEFTX

odbc_num_rows returns the correct result.

Both statements work as expected in the ADS development environment.

The rdms is db2 udb v8.1.9 linux

php is 4.4.0 with db2 compiled in
'./configure' '--prefix=/usr/local/php-4.4.0'
'--with-apxs2=/usr/local/apache2.0.54/bin/apxs'
'--with-ibm-db2=/db2home/db2inst1/sqllib'
'--with-openssl=/usr/local/ssl-0.9.8'


This is actually acceptable behavior for odbc_num_rows(). The PHP
documentation says as much:
http://de.php.net/manual/en/function.odbc-num-rows.php

I can't comment on the reasons why DB2 cannot return the number of rows
returned by the first statement.

There are some terrible suggestions in the comments to the function how to
get it to return some semi-reliable results (like running the query twice).
Maybe you could first explain what you need the row-count for. Then it
might be possible to give you a better advise.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 29 '05 #2
Knut Stolze wrote:
Bob Stearns wrote:

I am trying this query here because I got no useful reply on
alt.php.sql. If you can recommend a better venue for this question, I'd
be glad to hear it.

When I have an Sql statement of the form:

SELECT LOC_ID,REPEAT('--',LEVELX)||NAMEX
-- or SELECT LOC_ID,REPEAT('-',LEVELX*2)||NAMEX
FROM is3.locations
WHERE entity_id=2
AND (inactive='N' OR inactive IS NULL)
AND leftx BETWEEN 2 AND 983
ORDER BY LEFTX

odbc_num_rows returns -1. When I change the sql statement to:

SELECT LOC_ID,CHAR(LEVELX)||' '||NAMEX
FROM is3.locations
WHERE entity_id=2
AND (inactive='N' OR inactive IS NULL)
AND leftx BETWEEN 2 AND 983
ORDER BY LEFTX

odbc_num_rows returns the correct result.

Both statements work as expected in the ADS development environment.

The rdms is db2 udb v8.1.9 linux

php is 4.4.0 with db2 compiled in
'./configure' '--prefix=/usr/local/php-4.4.0'
'--with-apxs2=/usr/local/apache2.0.54/bin/apxs'
'--with-ibm-db2=/db2home/db2inst1/sqllib'
'--with-openssl=/usr/local/ssl-0.9.8'

This is actually acceptable behavior for odbc_num_rows(). The PHP
documentation says as much:
http://de.php.net/manual/en/function.odbc-num-rows.php

I can't comment on the reasons why DB2 cannot return the number of rows
returned by the first statement.

There are some terrible suggestions in the comments to the function how to
get it to return some semi-reliable results (like running the query twice).
Maybe you could first explain what you need the row-count for. Then it
might be possible to give you a better advise.

This is the first time I have had odbc_num_rows fail.

Just laziness: I use the count to judge the correctness of the result,
in particular I test (at various times) for n==0, n==1, n>0, n==0 ||
n==1. Furthermore, I use the construct

for($i=0; $i<$n; $i++) {
$arow = odbc_fetch_array($res, $i+1);
Nov 29 '05 #3
In one of Dan Scott's developerWorks articles on PHP and DB2
(http://www-128.ibm.com/developerwork...tt/index.html),
he mentions some cases where odbc_row_count() might not work.

--
--------------------
Larry Menard
"Defender of Geese and of All Things Natural"
"Bob Stearns" <rs**********@charter.net> wrote in message
news:M9*****************@fe06.lga...
Knut Stolze wrote:
Bob Stearns wrote:

I am trying this query here because I got no useful reply on
alt.php.sql. If you can recommend a better venue for this question, I'd
be glad to hear it.

When I have an Sql statement of the form:

SELECT LOC_ID,REPEAT('--',LEVELX)||NAMEX
-- or SELECT LOC_ID,REPEAT('-',LEVELX*2)||NAMEX
FROM is3.locations
WHERE entity_id=2
AND (inactive='N' OR inactive IS NULL)
AND leftx BETWEEN 2 AND 983
ORDER BY LEFTX

odbc_num_rows returns -1. When I change the sql statement to:

SELECT LOC_ID,CHAR(LEVELX)||' '||NAMEX
FROM is3.locations
WHERE entity_id=2
AND (inactive='N' OR inactive IS NULL)
AND leftx BETWEEN 2 AND 983
ORDER BY LEFTX

odbc_num_rows returns the correct result.

Both statements work as expected in the ADS development environment.

The rdms is db2 udb v8.1.9 linux

php is 4.4.0 with db2 compiled in
'./configure' '--prefix=/usr/local/php-4.4.0'
'--with-apxs2=/usr/local/apache2.0.54/bin/apxs'
'--with-ibm-db2=/db2home/db2inst1/sqllib'
'--with-openssl=/usr/local/ssl-0.9.8'

This is actually acceptable behavior for odbc_num_rows(). The PHP
documentation says as much:
http://de.php.net/manual/en/function.odbc-num-rows.php

I can't comment on the reasons why DB2 cannot return the number of rows
returned by the first statement.

There are some terrible suggestions in the comments to the function how
to
get it to return some semi-reliable results (like running the query
twice). Maybe you could first explain what you need the row-count for.
Then it
might be possible to give you a better advise.

This is the first time I have had odbc_num_rows fail.

Just laziness: I use the count to judge the correctness of the result,
in particular I test (at various times) for n==0, n==1, n>0, n==0 ||
n==1. Furthermore, I use the construct

for($i=0; $i<$n; $i++) {
$arow = odbc_fetch_array($res, $i+1);
.
.
.
}

Nov 29 '05 #4
Bob Stearns wrote:
Just laziness: I use the count to judge the correctness of the result,
in particular I test (at various times) for n==0, n==1, n>0, n==0 ||
n==1.
What do you use that for? You could also count the records you already got
and check those predicates that way, can't you?
Furthermore, I use the construct

for($i=0; $i<$n; $i++) {
$arow = odbc_fetch_array($res, $i+1);
.
.
.
}


Now there I'd use a while loop or something like:

for ($i = 0; $arow = odbc_fetch_array($res), $i++) {
...
}

Omitting the row number in the fetch is also a good idea because you could
then move a way from scrollable cursors and use forward-only ones (better
performance) without running into any suprises, like if $i changes inside
the loop.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 29 '05 #5
Knut Stolze wrote:
Bob Stearns wrote:

Just laziness: I use the count to judge the correctness of the result,
in particular I test (at various times) for n==0, n==1, n>0, n==0 ||
n==1.

What do you use that for? You could also count the records you already got
and check those predicates that way, can't you?

I haven't fetched any rows yet. I check n before I do any fetching.
Seems better if the sql statement is being run exactly for the value of n.
Furthermore, I use the construct

for($i=0; $i<$n; $i++) {
$arow = odbc_fetch_array($res, $i+1);
.
.
.
}

Now there I'd use a while loop or something like:

for ($i = 0; $arow = odbc_fetch_array($res), $i++) {
...
}

Omitting the row number in the fetch is also a good idea because you could
then move a way from scrollable cursors and use forward-only ones (better
performance) without running into any suprises, like if $i changes inside
the loop.

Hopefully, I don't ever change $i except in the for statement itself.
Will the loop run 0 times when the result set is empty?
Nov 29 '05 #6
Bob Stearns wrote:
Knut Stolze wrote:
Bob Stearns wrote:
Just laziness: I use the count to judge the correctness of the result,
in particular I test (at various times) for n==0, n==1, n>0, n==0 ||
n==1.


What do you use that for? You could also count the records you already
got and check those predicates that way, can't you?

I haven't fetched any rows yet. I check n before I do any fetching.
Seems better if the sql statement is being run exactly for the value of n.


I don't quite follow you there. I could do a fetch and if nothing comes
back, i.e. SQLCODE +100, then n == 0. Otherwise, there is something to do
anyways.
Now there I'd use a while loop or something like:

for ($i = 0; $arow = odbc_fetch_array($res), $i++) {
...
}

Omitting the row number in the fetch is also a good idea because you
could then move a way from scrollable cursors and use forward-only ones
(better performance) without running into any suprises, like if $i
changes inside the loop.

Hopefully, I don't ever change $i except in the for statement itself.
Will the loop run 0 times when the result set is empty?


The above loop won't do anything, i.e. run 0 times, if the fetch doesn't
return anything. It's the same is such a loop (imagine variables in the
proper places so that it makes sense):

for ($i = 1; $i < 1; $i++) { ... }

This loop won't do anything either.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 30 '05 #7
The only reason mysql_num_rows() returns the number of rows from a
SELECT statement is because MySQL immediately returns ALL of the rows
to the client for a mysql_query() call. Yes, this means that it's a bad
idea to do a SELECT that returns a million rows against MySQL. (Note
that the MySQL interface in PHP recently introduced
mysql_unbuffered_query() which returns only one row at a time -- and
surprise surprise, mysql_num_rows() will return 0 for a SELECT
statement when you call mysql_unbuffered_query().)

You have several options for getting the number of rows from a SELECT
statement in DB2:
1. Return all of the rows from the SELECT statement yourself, basically
explicitly doing the same thing that MySQL does under the covers:

while ($result_set[] = db2_fetch_array($stmt)) {}
$num_rows = count($result_set);

2. Very often, mysql_num_rows() is used only to determine whether any
results were returned from the SELECT statement. In this case, just go
ahead and issue your first db2_fetch_*() call -- if it returns FALSE,
then you know that there are no rows in the result set.

3. Use a scrollable cursor for your SELECT statement -- this uses more
resources on the database server but will allow db2_num_rows() to
return the right number of rows.

$stmt = db2_prepare($conn, 'SELECT firstnme FROM employee",
array("cursor" => DB2_SCROLLABLE));
db2_execute($stmt);
$num_rows = db2_num_rows($stmt);

Oh yeah, and please stay away from Unified ODBC for all of the reasons
mentioned in my article (the --with-ibm-db2 compile option) and use the
ibm_db2 extension instead (http://php.net/ibm_db2 or install the free
Zend Core for IBM from http://zend.com/core/ibm/ for precompiled PHP +
ibm_db2 extension). You'll have much more functionality and better
performance.

Nov 30 '05 #8
Dan Scott wrote:
The only reason mysql_num_rows() returns the number of rows from a
SELECT statement is because MySQL immediately returns ALL of the rows
to the client for a mysql_query() call. Yes, this means that it's a bad
idea to do a SELECT that returns a million rows against MySQL. (Note
that the MySQL interface in PHP recently introduced
mysql_unbuffered_query() which returns only one row at a time -- and
surprise surprise, mysql_num_rows() will return 0 for a SELECT
statement when you call mysql_unbuffered_query().)

You have several options for getting the number of rows from a SELECT
statement in DB2:
1. Return all of the rows from the SELECT statement yourself, basically
explicitly doing the same thing that MySQL does under the covers:

while ($result_set[] = db2_fetch_array($stmt)) {}
$num_rows = count($result_set);

2. Very often, mysql_num_rows() is used only to determine whether any
results were returned from the SELECT statement. In this case, just go
ahead and issue your first db2_fetch_*() call -- if it returns FALSE,
then you know that there are no rows in the result set.

I have written a general sql executor function which tests odbc_num_rows
for the conditions exactly 0, exactly 1, exactly 0 or 1, at least 1
returned result and fails/succeeds on the comparison. How do I do all of
these without impacting later fetches, especially if I'm trying to use
other advice I've gotten here: do not pass the row number to
odbc_fetch_array and use odbc_fetch_array for a loop predicate?
3. Use a scrollable cursor for your SELECT statement -- this uses more
resources on the database server but will allow db2_num_rows() to
return the right number of rows.

$stmt = db2_prepare($conn, 'SELECT firstnme FROM employee",
array("cursor" => DB2_SCROLLABLE));
db2_execute($stmt);
$num_rows = db2_num_rows($stmt);

Would a scrollable cursor be required if I wanted rows 125-149 of the
result? I make use of odbc_num_rows for driving such fetch operations as
well.
Oh yeah, and please stay away from Unified ODBC for all of the reasons
mentioned in my article (the --with-ibm-db2 compile option) and use the
A reference to the article would be appreciated.
ibm_db2 extension instead (http://php.net/ibm_db2 or install the free
Is either of these 1-1 substitutable with the corresponding odbc
library, by simply changing the odbc_* names to db2_* names?
Zend Core for IBM from http://zend.com/core/ibm/ for precompiled PHP +
ibm_db2 extension). You'll have much more functionality and better
performance.

Nov 30 '05 #9
Bob Stearns wrote:
Dan Scott wrote: I have written a general sql executor function which tests odbc_num_rows
for the conditions exactly 0, exactly 1, exactly 0 or 1, at least 1
returned result and fails/succeeds on the comparison. How do I do all of
these without impacting later fetches,
The you would need scrollable cursors to be able to scroll back after the
fetch in your counting function.
especially if I'm trying to use
other advice I've gotten here: do not pass the row number to
odbc_fetch_array and use odbc_fetch_array for a loop predicate?
I didn't say to avoid it at all costs. I just meant that you get often a
better performance if you stick to forward-only cursors.
Oh yeah, and please stay away from Unified ODBC for all of the reasons
mentioned in my article (the --with-ibm-db2 compile option) and use the


I'll second that. Especially if you try to use LOBs, you're in a lot of
trouble because the unified ODBC code screws up quite badly - no proper
error handling and a few other issues that Dan does not explain in his
article.
A reference to the article would be appreciated.


http://www-128.ibm.com/developerwork...614_scott.html

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 30 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: István Puskás | last post by:
Hi, I use an asp page to upload a word document to the web server (with Soft Artisans SA file-up), but sometimes it doesn't respond, seems to return with a zero sized answer. I use IIS 6...
11
by: John Eskie | last post by:
Lately I've seen alot of C and C++ code (not my own) which doesn't do any checking if memory obtained by new or malloc is valid or if they return NULL pointers. Why does most people not care about...
2
by: Frav | last post by:
The Reps team have been experiencing that Access 2002 unexpectedly quits while working and also lots of Corruption Failures and "Record lock can not update" messages since the upgrade from...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
1
by: shakeel | last post by:
Hi! to all, I have some problems using php with ODBC and access database i want ot get number of row in a table. code is like <?php $link=odbc_connect("myDSN","","") or die ("Failed");...
2
by: satnamsarai | last post by:
Using System.Net.Mail: Sometimes I get error 'failure sending mail. Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.' Not sure how...
1
by: vierling | last post by:
As a digibetic I don't know how to repair Jscript failure (run time failure rule 7 and 5): indicating : object is expected. This is what MS Script Editor tells me, but it doesnot tell me how to...
10
by: Caffeneide | last post by:
I'm using a php script which performs three xml queries to other three servers to retrieve a set of ids and after I do a query to mysql of the kind SELECT * FROM table WHERE id IN ('set of ids');...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.