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

Dear Monks,

I have been working on this for 4 days straight. I'm trying to get MySQL data. I've done it before in PHP but never in Perl. I can "show tables" (so I can access the DB) but nothing else works. Even using use CGI::Carp I just always get a blank page.

use DBI; use CGI::Carp qw(fatalsToBrowser); print "Content-type:text/html\n\n"; $db ="my_webapp2"; $user = "my_user"; $pass = "password"; $host="localhost"; $dbh = DBI->connect("DBI:mysql:$db:$host", $user, $pass); or die "Connecting to MySQL database failed: $DBI::errstr"; $SQLq = ("SELECT username FROM 'sessions' WHERE uid='1'"); $start = $dbh->prepare($SQLq) or die "Preparing MySQL query failed: $DBI::errstr "; $start->execute() or die "The execution of the MySQL query failed: $DBI::errstr"; $result = $start->fetchrow_hashref(); print "Ta-da!: $result->{username}\n"; while ($row = $start->fetchrow_hashref()); $dbh ->disconnect();

Replies are listed 'Best First'.
Re: Problems Getting MySQL Data
by Corion (Patriarch) on Oct 07, 2010 at 07:25 UTC

    What does your webserver error log say? Does your script run from the shell? Does it still run when you run it as the webserver user? Does a very simple script work that only outputs a constant string and does not connect to the database?

    You should maybe output some progress to the browser and also use { RaiseError => 1, PrintError => 0 }, so errors show up in your webserver error log but can't go undetected.

    I notice these lines in your code:

    $result = $start->fetchrow_hashref(); print "Ta-da!: $result->{username}\n";

    Maybe your table structure is not what you think it is or your resultset is empty. Try using Data::Dumper to output your structure. You should still see the "Ta-da!" being printed though.

    What is this line supposed to do? It has the effect of $start->finish() - is that what you meant to do?

    while ($row = $start->fetchrow_hashref());
      Hi,

      Here's what i would do, it probably can be improved but i get some output :)

      use strict; use warnings; use DBI; use CGI::Carp qw(fatalsToBrowser); print "Content-type:text/html\n\n"; my $db = "database"; my $user = "username"; my $pass = "password"; my $host = "localhost"; my $dbh = DBI->connect( "DBI:mysql:$db:$host", $user, $pass ) or die "Connecting to MySQL database failed: $DBI::errstr"; my $query = (" SELECT username FROM 'sessions' WHERE uid='1' "); my $start = $dbh->prepare($query) or die "Preparing MySQL query failed: $DBI::errstr"; $start->execute() or die "The execution of the MySQL query failed: $DBI::errstr"; my $result; while ($result = $start->fetchrow_hashref()) { print "Ta-da!:" . "$result->{username}\n"; } $dbh ->disconnect();
        Small improvement (easy if you keep a template on hand)
        #!/usr/bin/perl -- use strict; use warnings; use DBI; use CGI::Carp qw(fatalsToBrowser); Main(@ARGV); exit(0); sub Main { print "Content-type:text/html\n\n"; my $db = "database"; my $user = "username"; my $pass = "password"; my $host = "localhost"; my $dbh = DBI->connect( "DBI:mysql:$db:$host", $user, $pass ) or die "Connecting to MySQL database failed: $DBI::errstr"; my $query = ( " SELECT username FROM 'sessions' WHERE uid='1' " ); my $start = $dbh->prepare($query) or die "Preparing MySQL query failed: $DBI::errstr"; $start->execute() or die "The execution of the MySQL query failed: $DBI::errstr"; my $result; while ( $result = $start->fetchrow_hashref() ) { print "Ta-da!:" . "$result->{username}\n"; } $dbh->disconnect(); }
        Thank you! It works. I just wanted some output and I got some.

        (FYI for future readers: no quotes around the table name and column value worked for me.)
      I wasn't getting any error messages.
      What is this line supposed to do? It has the effect of $start->finish() - is that what you meant to do? while ($row = $start->fetchrow_hashref());
      I was tired. Nothing was working. I guess I was throwing in the kitchen sink by that time.--Not a good idea, I know.