in reply to Perl database access
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 |
|
||||||||
| a state | a city | a location | a description |
|
||||||||
| a state | a city | a location | a description |
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl database access
by Anonymous Monk on Sep 23, 2004 at 15:24 UTC | |
by ikegami (Patriarch) on Sep 23, 2004 at 16:04 UTC | |
|
Re^2: Perl database access
by Anonymous Monk on Sep 23, 2004 at 17:34 UTC | |
by ikegami (Patriarch) on Sep 23, 2004 at 18:02 UTC | |
by awohld (Hermit) on Sep 23, 2004 at 19:59 UTC |