Hi there,

I have a MySQL database and am using DBI to query it and a CGI
script to show the results in the browser.

My cgi takes input from the form (good) and performs as expected if the
input is a record in the database (good). However if nonsense is typed in - ie. the record
is not in the database, all I get is an internal sever error.

I tried putting counting the number of retrieved rows, figuring if there was nothing retrieved from the
database, $count would be zero. I then tried an if statement (if $count==0 then print stuff about record not found etc.)
This didn't help though.
#!/biol/programs/perl/bin/perl -w use strict; use DBI; use CGI qw(:standard); #print "Content-type:text/html\n\n"; my($dbh,$sth,$count,$sql,$cgi); $cgi = new CGI; use CGI::Carp qw(fatalsToBrowser); $dbh = DBI->connect("DBI:mysql:host=myhost;database=mydb", "username","password", {PrintError =>0, RaiseError => 1})|| die "Database connection not +made: $DBI::errstr"; $sql = qq{SELECT * FROM name}; $sth = $dbh->prepare($sql); $sth->execute(); print header(), start_html(-title=>"My Database",-BGCOLOR=>"white",-FONT=>"Times") +; print STDOUT "<H1>My Database</H1><BR><BR>"; print STDOUT "<TABLE><TR><TH>Short name</TH><TH>Long name</TH></TR +>"; my ($id,$short_name,$long_name); $sth->bind_columns(undef,\$id,\$short_name,\$long_name); while($sth->fetch()){ print STDOUT "<TR><TD>$short_name</TD><TD>$long_name<TD></TR>" +; } print STDOUT "</TABLE>"; print p (end_html); $sth->finish(); $dbh->disconnect(); exit(0);
Any ideas ?

thanks,
basm101

In reply to MySQL/DBI: Error if entry is not in the database by basm101

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.