Update: Was missing a FROM clause

my $dbh = DBI->connect( "DBI:mysql:$database:$db_server", $user, $password ); my $spdtrap_stmt = <<';'; SELECT id, state, city, discription FROM speedtrap WHERE state = ? ; my $comment_stmt = <<';'; SELECT comment_id, comments, name FROM comments WHERE id = ? ; # You need to make two dbh with many databases. # MySQL allows two queries to share the same dbh. # Create two dbh if need be. # Since we're using replaceable parameters, # We can prepare the comment sth once, and # executed multiple times. my $spdtrap_sth = $dbh->prepare($spdtrap_stmt) or die "Couldn't prepare the query: ".$DBI::errstr; my $comment_sth = $dbh->prepare($comment_stmt) or die "Couldn't prepare the comment query: ".$DBI::errstr; $spdtrap_sth->execute($state) or die "Couldn't execute query: ".$DBI::errstr; print <<EOF; <table border="1" align="center" width="100%"> <tr> <!-- why bother? -- <td width="1"> <p align="center">ID</p> </td> --> <td width="10%"> <p align="center">State</p> </td> <td width="15%"> <p align="center">City</p> </td> <td width="15%"> <p align="center">Location</p> </td> <td width="30%"> <p align="center">Description</p> </td> <td width="30%"> <p align="center">Comments</p> </td> </tr> EOF my @spdtrap_row; while (@spdtrap_row = $spdtrap_sth->fetchrow_array) { my ($id, $state, $city, $description) = @spdtrap_row; print <<EOF; <tr> <!-- why bother? -- <td>$id</td> --> <td>$state</td> <td>$city</td> <td>$location</td> <td>$description</td> <td><table border="0" width="100%"> <tr> <td width="70%">Comment</td> <td width="30%">By</td> </tr> EOF $comment_sth->execute($id) or die "Couldn't execute comment query: ".$DBI::errstr; my @comment_row; while (@comment_row = $comment_sth->fetchrow_array) { my ($comment_id, $comment_text, $commenter_name) = @comment_row; print <<EOF; <tr> <td>$comment_text</td> <td>$commenter_name</td> </tr> EOF } print <<EOF; </table></td> </tr> EOF } print "</table>"; $comment_sth->finish; $spdtrap_sth->finish; $dbh->disconnect;

Fixes:

output:

State

City

Location

Description

Comments

a state a city a location a description
Comment By
a comment_text a name
a comment_text a name
a comment_text a name
a state a city a location a description
Comment By
a comment_text a name
a comment_text a name
a comment_text a name
a state a city a location a description
Comment By
a comment_text a name
a comment_text a name
a comment_text a name


In reply to Re: Perl database access by ikegami
in thread Perl database access 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.