Hello all,
I am rather new to Perl and MySQL. I am using my summer break to work through a text on MySQL and Perl for the Web. I am having a bit of a problem with this piece of code. If anyone can see where it is wrong please let me know.
CODE
#! /usr/bin/perl -w
# intro6.pl - connect to MySql, retrieve data, write plain text output
use strict;
print "Content-type: text/html\n\n";
use DBI;
my ($db, $user, $pass, $hostn, $dsn, $dbh, $sth, $count);
$db="webdb";
$user="[i]username[/i]";
$pass="[i]password[/i]";
$hostn="[i]hostname[/i]";
$dsn = "DBI:mysqlatabase=$database;host=$hostname";
$dbh = DBI->connect($dsn, $user, $password);
$sth = $db ->prepare (SELECT name, wins, losses FROM teams");
$sth ->execute ();
$count = 0;
while (my @val = $sth ->fetchrow_array ()) {
print "name = %s, wins = %d, losses =%d\n",
$val[0], $val[1],
$val[2];
++$count;
}
print "$count rows total\n";
$sth ->finish ();
$db ->disconnect ();
exit (0);
The error I am receiving in my editor looks like this;
String found where operator expected at intro.pl line 17, near "print ""
(Might be a runaway multi-line "" string starting on line 13)
(Missing semicolon on previous line?)
Possible unintended interpolation of @val in string at intro.pl line 13.
Bareword found where operator expected at intro.pl line 17, near "print "name"
(Do you need to predeclare print?)
Backslash found where operator expected at intro.pl line 17, near "%d\"
(Missing operator before \?)
String found where operator expected at intro.pl line 22, near "print ""
(Might be a runaway multi-line "" string starting on line 17)
(Missing semicolon on previous line?)
Scalar found where operator expected at intro.pl line 22, near "print "$count"
Global symbol "$database" requires explicit package name at intro.pl line 11.
Global symbol "$hostname" requires explicit package name at intro.pl line 11.
Global symbol "$password" requires explicit package name at intro.pl line 12.
syntax error at intro.pl line 17, near "print ""
Global symbol "@val" requires explicit package name at intro.pl line 13.
Global symbol "%s" requires explicit package name at intro.pl line 17.
Global symbol "%d" requires explicit package name at intro.pl line 17.
Global symbol "%d" requires explicit package name at intro.pl line 17.
Global symbol "@val" requires explicit package name at intro.pl line 17.
Global symbol "@val" requires explicit package name at intro.pl line 17.
Global symbol "@val" requires explicit package name at intro.pl line 17.
intro.pl has too many errors.Any help would be greatly appreciated.
I'm so sorry! I found the error almost immediately after posting this. I was missing a double quote in my SELECT clause.