in reply to Problems Getting MySQL Data

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());

Replies are listed 'Best First'.
Re^2: Problems Getting MySQL Data
by longjohnsilver (Acolyte) on Oct 07, 2010 at 13:08 UTC
    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(); }
        Thanks for the template link. I'll be using it often.
      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.)
Re^2: Problems Getting MySQL Data
by Anonymous Monk on Oct 08, 2010 at 00:47 UTC
    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.