Well, there are two things wrong with your prepare/execute. First of all, putting $dbh->errstr inside of a double-quoted string doesn't do what you want it to (it stringifies $dbh, and then it just keeps "->errstr" as part of the literal string). That should be:
... or die "Couldn't execute query: ".$dbh->errstr;
So that you can get some useful debugging information from the database.

Second (just to take a wild guess here), your query string looks something like "...&state=TX&..." which means that the way you need to generate your sql statement is more like:

$statement = "SELECT city FROM database WHERE state='$FORM{state}'";
Of course, this would be very BAD because you're not stripping single-quotes from your state, and besides, queries should use bound placeholders (in general). So what it should actually be is:
$statement = "SELECT city FROM database WHERE state=?"; $sth = $dbh->prepare($statement) or die "Couldn't prepare the query: " +.$sth->errstr; $rv = $sth->execute($FORM{state}) or die "Couldn't execute query: ".$d +bh->errstr;
If that's over your head, the short answer is that that question-mark in the query works kind of like a %d would in printf. Anyway, you should read the DBI docs about placeholders (also called "bind variables" or "binds") for more info.

Oops... see UPDATE comment below.

Last, you're actually going to want to fetch the data from that query and do something with it. That will probably look something like (and there are many ways to do this... but here's one way):

# inside the html <table> foreach my $row ($sth->fetchall_arrayref) { my $state = $row->[0]; print "... some html or other based on $state..."; }

Anyway, it appears that you're gonna want to do some documentation reading and possibly looking at more example code.

Oh... and I didn't bother to look any further down than the query, because that is clearly going wrong right now... you need to resolve this problem before you worry about how you the html in exactly whatever way you want. Learn to walk before you learn to run, and all.

Good luck.

P.S. Oh, and I'll leave the rant about how you should be using CGI.pm to someone else :-)

UPDATE: Actually, I made a mistake by not looking closely enough all the way to the bottom... you are fetching the results (which is good). BUT since you are calling $sth->finish and $dbh->disconnect at the top of the script and doing your fetching at the bottom of the script (after finishing and disconnecting), that's gonna break. (Also, this is why I missed the fetching down below... I assumed (like the code would) that once you have disconnect from the DB, you're not doing any further calls against the DB.) What this is means is: move the finish and disconnect to the bottom after you have fetched.

------------ :Wq Not an editor command: Wq

In reply to Re: Trying to print HTML table, erroring out. by etcshadow
in thread Print Array into 4 Column Table by Anonymous Monk

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.