stonecolddevin has asked for the wisdom of the Perl Monks concerning the following question:
Hey all,
I'm using CGI::Application and DBI with MySQL to create a message board of sorts.
CGI::Application has surpassed my expectations as far as organizing my code and cutting down development time, and with that same thought in mind, I decided to toss out Class::DBI for speed. Once again this has worked.
Now the problem.
I'm attempting to retrieve a reply count on each individual thread to show in the main page.
Originally, I used
where @info was the array of data retrieved by Class::DBI, and $obj->DBI->Replies->sql_count was defined in PL::DBI::Replies asfor (@info) { my %data; my $sth = $obj->DBI->Replies->sql_count; $sth->execute($_->id); my $r = ($sth->fetchrow_array)[0]; $data{author} = $_->author; $data{content} = $_->content; $data{title} = $_->title; $data{id} = $_->id; $data{date} = $_->date; $data{count} = $r; push @loop_data, \%data; }
and of course, the original thread id was passed to the placeholder.SELECT COUNT(*) from __TABLE__ where thread_id=?
What I am trying now, and it's obviously not working, is this:
#get the reply count... my $sth2 = $dbh->prepare(q{ select count(*) from dh_replies where th +read_id=?}); for ( scalar $rows_data ) { $sth2->execute($_); my $count = $sth2->fetchrow_array; push @$rows_data, { count => $count }; }
I can't really tell you how I think this is going wrong, thus the reason I brought it before You, the illustrious monks.
UPDATE: Ok I lied. I think where I'm going wrong has to do with scalar $rows_data, and then $sth->execute($_)
Also, push @$rows_data, { count => $count } doesn't seem to be working either...
UPDATE^2: This works, but I'm sure there's a more efficient way to do it:
### get replies ### use each $row's id as the argument passed to execute and retri +eve ### reply counts on each message my $sth2 = $dbh->prepare(q{ select count(*) from dh_replies where +thread_id=? }); $sth2->execute( $rows->{'id'} ); my $count = ($sth2->fetchrow_array)[0]; my %data; $data{'subject'} = $rows->{'subject'}; $data{'author'} = $rows->{'author'}; $data{'content'} = $rows->{'content'}; $data{'date'} = $rows->{'date'}; $data{'id'} = $rows->{'id'}; $data{'count'} = $count; push @rows_data, \%data; }
Please help me if you can, if any clarifications are needed let me know.
-devin
|
|---|