1 Replies - 3199 Views - Last Post: 26 June 2009 - 08:23 PM

#1 JMM75   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-December 08

Connecting to MySQL

Post icon  Posted 25 June 2009 - 09:30 PM

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.

#! /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.

Is This A Good Question/Topic? 0
  • +

Replies To: Connecting to MySQL

#2 GWatt   User is offline

  • member icon

Reputation: 309
  • View blog
  • Posts: 3,106
  • Joined: 01-December 05

Re: Connecting to MySQL

Posted 26 June 2009 - 08:23 PM

At least one of those errors is because on line 13 you have an ending quote, but no starting quote.
$sth = $db->prepare(SELECT name, wins, loses FROM teams");
There should be an opening quote in there before SELECT
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1