|
|
Question : Sybase returning row numbers
|
|
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?
|
Answer : Sybase returning row numbers
|
|
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...
|
|
|
|
|