Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl + SQL question

Status
Not open for further replies.

c4n

Programmer
Mar 12, 2002
110
SI
In my Perl script I use

while ($h = $sth->fetchrow_hashref) {
do this and that with $h->{colum1}, $h->{colum2} etc.
}

to print out the results of a SELECT statement.

My question is how can I know (get the number of) how many results (rows) are returned before I begin the "while" command?

Thank you!
 
I don't think you can. Seems like I remember reading somewhere that the the record sets are typically returned by db servers in chunks or groups of records.(I don't remember where I read it, maybe on the dbi-users list??) Two fairly straight forward approaches might be....

1 - pull all results into a memory structure before you do any ouput... using the memory structure as a temp container while you play with the data.

2 - execute two queries, first to get result set size and second to output the records as appropriate.
Code:
select count(*) from ... where condition....

select field1, field2 from ... where same condition.....
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Thanks, i will probably use the 2nd suggestion.

I did run by this statement in MySQL reference, but can't get it working ...

SELECT CALC_FOUND_ROWS * FROM table_name WHERE id > 100 LIMIT 10;
SELECT FOUND_ROWS();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top