Hello, Monks. I'm trying to write a threaded message board (Matt Wright's wwwboard) with perl DBI, and mySQL. The below code function sucks, and I'm trying to improve it. It works, however, I am concered about my compiling a prepare statement over and over in the below function:

sub get_thread_replies { my ($T_id, $M_id) = ($_[0], $_[1]); my $sth1 = $dbh->prepare("SELECT * FROM $vars{'poststable'} WHERE thre +ad_id = ? AND level_id = 0 AND parent_id = ? ORDER by message_id DESC +"); $sth1->execute($T_id, $M_id); $sth1->bind_col(1, \$message_id); $sth1->bind_col(2, \$parent_id); $sth1->bind_col(3, \$thread_id); $sth1->bind_col(4, \$level_id); $sth1->bind_col(5, \$name); $sth1->bind_col(6, \$email); $sth1->bind_col(7, \$location); $sth1->bind_col(8, \$icon); $sth1->bind_col(9, \$subject); $sth1->bind_col(10, \$body); $sth1->bind_col(11, \$message_url); $sth1->bind_col(12, \$message_url_title); $sth1->bind_col(13, \$datetime); $sth1->bind_col(14, \$user_icon); $sth1->bind_col(15, \$user_sig); $sth1->bind_col(16, \$reply_subject); while ( $sth1->fetch ) { print "<ul><li><b><a href=\"viewpost.cgi?thread_id=$thread_id&message_ +id=$message_id\">$subject</a></b> <img src=\"$vars{'boardiconsdirurl' +}/$icon.gif\" border=0> <b><font color=\"black\">$name</font></b> <s +mall><font color=\"dimgray\">$location </small> </font>$datetime\n"; &get_thread_replies($thread_id, $message_id); print "</ul>\n"; } }

The function recursively goes through the table looking for replies to a parent message, running the function again, until there are no more replies for that message id. I want to run the prepare statement however just once, and use execute thereafter, and not do what I am doing which is calling a prepare statement each time the function is called. Red------


In reply to Message Board Threading with DBI/MySQL by Red Neckerson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.