Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Help!!!

Here's the code. I don't get any data returned.

#!/usr/bin/perl -w # perl modules use CGI; use DBI; use strict; use diagnostics; # setup CGI handle my $cgi = new CGI; # start HTML print $cgi->header; my $dsn = 'DBI:mysql:mydb:localhost'; my $db_user_name = 'mydbusername'; my $db_password = 'mydbpassword'; my $dbh = DBI->connect($dsn, $db_user_name, $db_password); my $sth = $dbh->prepare(qq{select user, topic, topictest from pagedata +}); $sth->execute(); while (my ($username, $topic, $topictext) = $sth->fetchrow_array()) { print "$username, $topic, $topictext"; } $sth->finish(); $dbh->disconnect();

edited: Thu Oct 24 14:54:48 2002 by jeffa - added code tags and s/<br>//g

Replies are listed 'Best First'.
Re: DBI Problem
by Zaxo (Archbishop) on Oct 23, 2002 at 00:15 UTC

    I think your $dsn is incorrect, try: my $dsn = 'DBI:mysql:database=mydb;host=localhost';

    It would be a good idea to check for errors after each DBI call. You can set that up to be automatic with the 'RaiseError' attribute in the connect() call.

    After Compline,
    Zaxo

      I agree. In the original code, not so much as an error check after the initial connection. Here is the same script, with (minimal) error checking:
      #!/usr/bin/perl -w # perl modules use CGI; use DBI; use strict; use diagnostics; # setup CGI handle my $cgi = new CGI; # start HTML print $cgi->header; my $dsn = 'DBI:mysql:mydb:localhost'; my $db_user_name = 'mydbusername'; my $db_password = 'mydbpassword'; my $dbh = DBI->connect($dsn, $db_user_name, $db_password) or die "Coul +dn't connect to $dsn: $DBI::errstr"; my $sth = $dbh->prepare(qq{select user, topic, topictest from pagedata +}) or die "Couldn't prepare select: ", $dbh->errstr; $sth->execute() or die "Couldn't execute query: ", $dbh->errstr; while (my ($username, $topic, $topictext) = $sth->fetchrow_array()) { print "$username, $topic, $topictext"; } $sth->finish(); $dbh->disconnect();

      thor

Re: (nrd) DBI Problem
by newrisedesigns (Curate) on Oct 23, 2002 at 00:15 UTC

    I'm learning DBI as well. This might be of some help to you.

    I have a hunch it might be something with your $dsn variable and the connect() call.

    From the article (by monk Dominus):

    use DBI; my $dbh = DBI->connect('DBI:Oracle:payroll', 'username', 'password') or die "Couldn't connect to database: " . DBI->errstr;

    John J Reiser
    newrisedesigns.com

Re: DBI Problem - NEVER MIND
by Anonymous Monk on Oct 23, 2002 at 00:08 UTC
    ARRGH! My humble apologies... it was a type in the field name which caused the whole query to return noghting.
      typo?