School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,456 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,627 people online right now. Registration is fast and FREE... Join Now!




Connecting to MySQL

 

Connecting to MySQL

JMM75

25 Jun, 2009 - 08:30 PM
Post #1

New D.I.C Head
*

Joined: 29 Dec, 2008
Posts: 4


My Contributions
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.


User is offlineProfile CardPM
+Quote Post


GWatt

RE: Connecting To MySQL

26 Jun, 2009 - 07:23 PM
Post #2

//
Group Icon

Joined: 1 Dec, 2005
Posts: 2,496



Thanked: 61 times
Dream Kudos: 525
My Contributions
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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 02:04AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month