in reply to perl/CGI/DBI + odd behaviour

I'm a bit confused by your post. In the error message you provide,
DBI->connect(host=myhost;database=my_db) failed: Access denied for u +ser: 'maisey@myhost' (Using password: NO) at frontpage.cgi line 25
the host and database don't correspond to the code you posted
DBI->connect("DBI:mysql:host=localhost;database=db_name", ...
This leads me to believe that the error might be coming from somewhere else.

Suggestion: Strip this down to a small example that demonstrates the problem. You don't need to show us your table formatting code, just a snippet that exhibits this error message, but still permits a query to be executed without error. I'm betting you can do this in less than 20 lines. If you haven't spotted the problem yourself along the way, a small snippet increases the odds that someone here will spot the problem.

Replies are listed 'Best First'.
Re: Re: perl/CGI/DBI + odd behaviour
by Anonymous Monk on Feb 19, 2003 at 19:02 UTC
    ok, here's a condensed version of the code. also the error message having a different host name to that in the script was just a typo. cheers, maisie x
    #THE LOGIN PAGE *********************** #!/biol/programs/perl/bin/perl -w use strict; use DBI; use CGI qw(:standard); my $cgi = new CGI; use CGI::Carp qw(fatalsToBrowser); print "Content-type:text/html\n\n"; print <<EOF; <h2>database</h2><BR> <FORM METHOD="post" ACTION="http://myhost/~maisie/cgi-bin/database/ +frontpage.cgi"> <h3>Enter user name</h3> <INPUT TYPE="TEXT" NAME="username_box" SIZE="10"> <h3>Enter password</h3><INPUT TYPE="password" NAME="password_box" S +IZE +="10"> <INPUT TYPE="SUBMIT" value="Log in"> </FORM> </BODY></HTML> EOF
    The next script connects to the database and searches it;
    #!/biol/programs/perl/bin/perl -w use strict; use DBI; use CGI qw(:standard); my $cgi = new CGI; my $username = $cgi->param('username_box'); my $password = $cgi->param('password_box'); my ($sth, $sth2); my $dbh = DBI->connect("DBI:mysql:host=myhost;database=my_db", "$us +e +rname","$password", {PrintError =>0, RaiseError => 1}) || die "Dat +aba +se connection not made"; my $plate = $cgi->param("plate"); print $cgi->start_form (-method => "POST"); print $cgi->textfield (-name => "plate", -value => $plate,-size => +40); print $cgi->submit (-name => "button", -value=>"search"); print $cgi->end_form (); print "Search results for keyword: $plate \n", $cgi->escapeHTML; $sth2 = $dbh->prepare (qq{ SELECT * FROM plate WHERE plate_id LI +KE +? }); $sth2->execute("%" . $plate . "%" ); my $sql = qq{select * from plate}; $sth = $dbh->prepare ($sql); $sth->execute(); while (my $row = $sth->fetchrow_arrayref) { print join ("\t", @$row), "<P>"; } $dbh->disconnect(); print $cgi->end_html ();