Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: DBI, From PHP to Perl

by tantarbobus (Hermit)
on Nov 08, 2003 at 19:11 UTC ( #305577=note: print w/replies, xml ) Need Help??


in reply to DBI, From PHP to Perl

$sth->rows() is what you want, but you should be advised that $sth->rows() is not guaranteed to give you an accurate count until you fetch all of the rows in the result set. With MySQL you should be fine calling $sth->rows() before you fetch all of the rows from the database unless you set the mysql_use_result attribute on the sth, but then again you would have the same problem with php if you used mysql_use_result().

So you can do something like this:
$sth = $dbh->prepare($sql); while (my @results = $sth->fetchrow_array) { do_something(@results) } my $count = $sth->rows();

You probably will also want to 'use strict' and 'use warnings' in your code. They will make your code easier to debug because they keep you from shooting yourself in foot in some of the most common ways. One other thing you might want to look at is Template-Toolkit or HTML::Template both of wich will allow you to remove the html from you code, so that your program will be easier to read.

Replies are listed 'Best First'.
Re: Re: DBI, From PHP to Perl
by dstefani (Sexton) on Nov 08, 2003 at 19:29 UTC
    I guess I'm not being clear, excuse me.
    I need the count before hand, not after. So I guess what I'm doing now is as good as it gets.
    Thanks though.
    dstefani

    BTW: (strict, -w) all on :-) ...chunk of code...

      While I'm still not sure why you need to know how many rows are being returned, you should look at fetchall_arrayref in DBI (look in the section 'Statement Handle Methods'). This will return your entire recordset into an array of array references (one per record).

      In your original post, you mentioned that you are using the number of records returned to format your table when you paint the results. Do you really need the record count beforehand or can you define your rules for making your results look nice and use them dynamically while you work through your record set?

      Oren

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://305577]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2019-10-20 02:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?