in reply to Re: Problems Getting MySQL Data
in thread Problems Getting MySQL Data

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

Replies are listed 'Best First'.
Re^3: Problems Getting MySQL Data
by Anonymous Monk on Oct 07, 2010 at 13:32 UTC
    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.
Re^3: Problems Getting MySQL Data
by Anonymous Monk on Oct 08, 2010 at 00:50 UTC
    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.)