Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sybase returning row numbers

Posted on 2001-07-08
6
2,984 Views
Last Modified: 2007-11-27
How can I get sybperl to give me the row count after executing an SQL Query (a "SELECT" query, for example).

If I query a database with ct_execute, I then call:

$count = $dbh -> ct_res_info(CS_NUMDATA)

...but $count seems to give me completely arbitrary values (i.e.: 15 for 10 rows, 58 for 713 rows, 180
for no results, etc..)

Am I missing something here?
0
Comment
Question by:Tonyboy
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:amitpagarwal
ID: 6263674
Well - it would: that's the number of columns in the result set, not the number of rows. Use CS_ROW_COUNT instead.


 $rows = $dbh->ct_res_info(CS_ROW_COUNT);
0
 

Author Comment

by:Tonyboy
ID: 6265650
Hmmm... I agree with your solution, but given the following Perl script (I hope you're familiar):

if ($restype == CS_ROW_RESULT) {
     $sql_rows = $dbh -> ct_res_info(CS_ROW_COUNT) || 1;
}

For some reason, though, CS_ROW_COUNT never returns any value, and so the 1 is used (if I put 2 there, the 2 would be used, etc...)
0
 
LVL 5

Accepted Solution

by:
amitpagarwal earned 200 total points
ID: 6265688
It's a protocol thing... Sybase doesn't know (or doesn't tell the APIs) how many rows are returned until all the rows have been fetched,   so you can't know in advance how many rows will be returned by a query    unless you do a select count(*) first.

 The CS_ROW_COUNT property is really usefull for updates/inserts/deletes,   though...

That being said, the CS_ROW_COUNT attribute is retrievable once

                          ct_results returns a $restype of CS_CMD_DONE, so you should
                          write a loop to get the number of rows affected, like this:

                          $dbh->ct_execute($query);
                          while($dbh->ct_results($restype) == CS_SUCCEED) {
                              if($restype == CS_CMD_DONE) {
                                 $numrows = $dbh->ct_res_info(CS_ROW_COUNT);
                              }
                              # do any other processing here
                              # for example, process any fetchable rows:
                              if($dbh->ct_fetchable($restype)) {
                                 while(@dat = $dbh->ct_fetch) {
                                    # do whatever you need to do with the data...
                                 }
                               }
                          }


                          Hope this helps a little bit...
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Tonyboy
ID: 6265748
Oh... That's dissapointing..

Is there any way of finding out how many rows are returned before fetching all of the results?
0
 
LVL 5

Expert Comment

by:amitpagarwal
ID: 6265754
listening ..
0
 

Author Comment

by:Tonyboy
ID: 6265798
???
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most MSPs worth their salt are already offering cybersecurity to their customers. But cybersecurity as a service is wide encompassing and can mean many things.  So where are MSPs falling in this spectrum?
This article describes how to reset your Windows 10 password when you've forgotten it.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question