stonecolddevin has asked for the wisdom of the Perl Monks concerning the following question:

Howdy howdy howdy,
I have this message board script for my forum which I am constantly modifying that won't fetch the messages column's data.

Here is the script:

#!perl use strict; use lib "C:/apache/htdocs/pm"; use my_board; my( $msg_id, $sth, $count, $msg, $resp ); if ( $CG->param( 'msg' ) =~ /^(\d+)$/ ) { $msg_id = $1; } else { dienice( $CG->param( 'msg' ) ."isn't a valid message number." ); } $sth = $dbh->prepare( "SELECT messages.*, date_format(date, '%c/%e/%Y/ +%r') AS nicedate, forums.name FROM messages, forums WHERE messages.fo +rum = forums.id and (messages.id=? OR thread_id=?) ORDER BY thread_id +, date" ) or dbdie(); $sth->execute( $msg_id, $msg_id); if ( $sth < 1 ) { dienice( "Message $msg_id does not exist" ); } $msg = $sth->fetchrow_hashref; # Create the header create_header("Message #$msg_id: $msg->{subject}"); print $CG->a({-href=>'forums.cgi?forum=' . $msg->{forum}}, 'Topic: +&nbsp;' . $msg->{subject}) . "\n" . $CG->br() . "\n"; showpst( $msg ); print $CG->hr() . "\n"; print $CG->a( {-href=>'reply.cgi?forum=' . $msg->{forum} . '&msg=' + . $msg->{id}}, 'Reply to this post') . "\n" . $CG->br() . "\n"; print $CG->p('Replies: ') . "\n" . $CG->br() . "\n"; showrep( $msg ); sub showpst { my ($hd); my ($msg1) = shift; # Change carriage returns to <BR>'s $msg->{message} = s/\n/<br>/; print qq{<!--[Start of post]-->}; print $CG->hr() . "\n"; print $CG->p('Message #: ' . $msg->{id}) . "\n" . $CG->br() . "\n" +; print $CG->p('Subject: ' . $msg->{subject}) . "\n" . $CG->br() . " +\n"; print $CG->p('Author: ' . $msg->{author}) . "\n" . $CG->br() . "\n +"; print $CG->p('Posted on: ' . $msg->{nicedate}) . "\n" . $CG->br() +. "\n"; print $CG->p($msg->{author} . "'s message:") . "\n" . $CG->br() . +"\n"; print $CG->p($msg->{message}) . "\n" . $CG->br() . "\n"; print qq{<!--[End of post]-->} . "\n"; print qq{<!--[Replies:]-->} . "\n"; } sub showrep { $sth = $dbh->prepare( "SELECT replies.*, date_format(date, '%c/%e/ +%Y/%r') AS nicedate, forums.name FROM messages, forums WHERE replies. +forum = forums.id and (replies.id=? OR thread_id=?) ORDER BY thread_i +d, date" ) or dbdie(); $sth->execute( $msg_id, $msg_id); }

It will fetch EVERYTHING else (with the exception of replies, don't have that working yet), but not messages. Any ideas?
thank you in advance.
Dhoss

And if you're feeling lucky... come and take me home And if you feel loved If you feel lucky, if you feel loved If you feel lucky, if you feel loved You've crossed the walls - Excelled Further along through their hell All for my heart, I watch you kill You always have, you always will Now spread your wings and sail out to me....

Replies are listed 'Best First'.
Re: Script won't retrieve messages column
by dws (Chancellor) on Jul 11, 2003 at 23:50 UTC
    It will fetch EVERYTHING else (with the exception of replies, don't have that working yet), but not messages. Any ideas?

    1. Triple check that the field name is really "message".
    2. Examine keys %$msg to see what is being returned.
    3. use strict; to avoid accidentally using globals when you think you aren't, as you're doing inadvertently when calling showpst() (Hint: Are you really using that argument?)
Re: Script won't retrieve messages column
by saintbrie (Scribe) on Jul 12, 2003 at 02:32 UTC
    Does the query execute correctly from the command line?
Re: Script won't retrieve messages column
by archon (Monk) on Jul 11, 2003 at 23:10 UTC
    $sth->execute( $msg_id, $msg_id);

    shouldn't you have two different variables there?

      Well, I'm not totally sure, because most of the SQL is out of a forum builder book, so I'm not 100% sure on that, I'll go back and check, otherwise I'll have to re-write the SQL stuff then.

      And if you're feeling lucky... come and take me home And if you feel loved If you feel lucky, if you feel loved If you feel lucky, if you feel loved You've crossed the walls - Excelled Further along through their hell All for my heart, I watch you kill You always have, you always will Now spread your wings and sail out to me....
        Well, I'm not totally sure, because most of the SQL is out of a forum builder book, so I'm not 100% sure on that, I'll go back and check, otherwise I'll have to re-write the SQL stuff then.

        Well if messages.id or thread_id are the same thing then it shouldnt matter, but most likely the thread id is just the parent message id to keep track of threads that are related...i doubt this is your problem

        being that this is code from a book, have you verified that your db tables messages or forums have a messages.* field? maybe by making a single simple query for fields that start with messages.?
        -Robert

        Hopefully there will be a witty sig here at some point