Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Beginner DBI problem

Reply
Thread Tools

Beginner DBI problem

 
 
poopdeville@gmail.com
Guest
Posts: n/a
 
      10-19-2005
Hi everybody.

I'm having a bit of a problem getting DBI to interface with mysql.
This is my first experience with DBI, so I'm not really sure where to
start troubleshooting. I've read several FMs, but my -> foo isn't so
great. Basically, I'm trying to pull out a column of data out of a
mysql table and printing HTML based on it. A minimal example of code
that causes trouble is:

# use DBI;
#
# my $dbh = DBI->connect('DBI:mysql:database', 'user', 'pass')
# or die "Couldn't connect to database: " . DBI->errstr;
# my $sth = $dbh->do('SELECT title FROM pages') or die
# "Couldn't query the database: " . DBI->errstr;

# while(my @title = $sth->fetchrow_array) {
# print "<option>$title[0]</option>\n";
# }

(It's commented to preserve formatting.) I get an error: Can't call
method "fetchrow_array" without a package or object reference at... the
line where the while loop starts. Can anybody help?

Thanks,
'cid 'ooh

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-19-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in
news:(E-Mail Removed) ups.com:

> Hi everybody.
>
> I'm having a bit of a problem getting DBI to interface with mysql.
> This is my first experience with DBI, so I'm not really sure where to
> start troubleshooting. I've read several FMs, but my -> foo isn't so


What is an "FM"?

What is a "-> foo"?

> great. Basically, I'm trying to pull out a column of data out of a
> mysql table and printing HTML based on it. A minimal example of code
> that causes trouble is:
>
> # use DBI;
> #
> # my $dbh = DBI->connect('DBI:mysql:database', 'user', 'pass')
> # or die "Couldn't connect to database: " . DBI->errstr;
> # my $sth = $dbh->do('SELECT title FROM pages') or die
> # "Couldn't query the database: " . DBI->errstr;

....
> (It's commented to preserve formatting.) I get an error: Can't call
> method "fetchrow_array" without a package or object reference at...
> the line where the while loop starts. Can anybody help?


The do method returns the number of rows affected, not a statement
handle.

What you need is a prepare and execute.

Consult the DBI docs for examples.

Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and
remove .invalid for email address)

comp.lang.perl.misc guidelines on
the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      10-19-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi everybody.
>
> I'm having a bit of a problem getting DBI to interface with mysql.
> This is my first experience with DBI, so I'm not really sure where to
> start troubleshooting. I've read several FMs, but my -> foo isn't so
> great. Basically, I'm trying to pull out a column of data out of a
> mysql table and printing HTML based on it. A minimal example of code
> that causes trouble is:
>
> # use DBI;
> #
> # my $dbh = DBI->connect('DBI:mysql:database', 'user', 'pass')
> # or die "Couldn't connect to database: " . DBI->errstr;


Always be explicit:

my $dbh = DBI->connect('DBI:mysql:database', 'user', 'pass', { RaiseError =>
1, AutoCommit => 0 } )
or die "Could not connect to database: " . DBI->errstr;

> # my $sth = $dbh->do('SELECT title FROM pages') or die
> # "Couldn't query the database: " . DBI->errstr;


Please read the documentation for any function you don't understand. No one
likes to find out there problem is blatantly obvious after wasted time
debugging:

<quote for do>
Prepare and execute a single statement. Returns the number of rows affected
or undef on error.
</quote>

You don't get a statement handle back from do, so you obviously can't call
any methods on return value.

my $sth = $dbh->prepare("SELECT title FROM pages") or die DBI->errstr;


Matt


 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      10-19-2005

"Matt Garrish" <(E-Mail Removed)> wrote in message
news:S5i5f.10550$(E-Mail Removed). ..
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>> Hi everybody.
>>
>> I'm having a bit of a problem getting DBI to interface with mysql.
>> This is my first experience with DBI, so I'm not really sure where to
>> start troubleshooting. I've read several FMs, but my -> foo isn't so
>> great. Basically, I'm trying to pull out a column of data out of a
>> mysql table and printing HTML based on it. A minimal example of code
>> that causes trouble is:
>>
>> # use DBI;
>> #
>> # my $dbh = DBI->connect('DBI:mysql:database', 'user', 'pass')
>> # or die "Couldn't connect to database: " . DBI->errstr;

>
> Always be explicit:
>
> my $dbh = DBI->connect('DBI:mysql:database', 'user', 'pass', { RaiseError
> => 1, AutoCommit => 0 } )
> or die "Could not connect to database: " . DBI->errstr;
>
>> # my $sth = $dbh->do('SELECT title FROM pages') or die
>> # "Couldn't query the database: " . DBI->errstr;

>
> Please read the documentation for any function you don't understand. No
> one likes to find out there problem is blatantly obvious after wasted time
> debugging:
>
> <quote for do>
> Prepare and execute a single statement. Returns the number of rows
> affected or undef on error.
> </quote>
>
> You don't get a statement handle back from do, so you obviously can't call
> any methods on return value.
>
> my $sth = $dbh->prepare("SELECT title FROM pages") or die DBI->errstr;
>


And of course...

$sth->execute() or die DBI->errstr;

Matt


 
Reply With Quote
 
poopdeville@gmail.com
Guest
Posts: n/a
 
      10-19-2005

A. Sinan Unur wrote:
> (E-Mail Removed) wrote in
> news:(E-Mail Removed) ups.com:
>
> > Hi everybody.
> >
> > I'm having a bit of a problem getting DBI to interface with mysql.
> > This is my first experience with DBI, so I'm not really sure where to
> > start troubleshooting. I've read several FMs, but my -> foo isn't so

>
> What is an "FM"?


"****ing manuals," compare with "RTFM."
>
> What is a "-> foo"?


"Arrow-foo," compare with kung-fu.

> > great. Basically, I'm trying to pull out a column of data out of a
> > mysql table and printing HTML based on it. A minimal example of code
> > that causes trouble is:
> >
> > # use DBI;
> > #
> > # my $dbh = DBI->connect('DBI:mysql:database', 'user', 'pass')
> > # or die "Couldn't connect to database: " . DBI->errstr;
> > # my $sth = $dbh->do('SELECT title FROM pages') or die
> > # "Couldn't query the database: " . DBI->errstr;

> ...
> > (It's commented to preserve formatting.) I get an error: Can't call
> > method "fetchrow_array" without a package or object reference at...
> > the line where the while loop starts. Can anybody help?

>
> The do method returns the number of rows affected, not a statement
> handle.
>
> What you need is a prepare and execute.
>
> Consult the DBI docs for examples.


Thanks for your help. It works now!

'cid 'ooh

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      10-19-2005
Matt Garrish wrote:
>
> $sth->execute() or die DBI->errstr;


Shouldn't that be

$sth->execute() or die $sth->errstr;

? Or doesn't it matter?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      10-19-2005

"Gunnar Hjalmarsson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Matt Garrish wrote:
>>
>> $sth->execute() or die DBI->errstr;

>
> Shouldn't that be
>
> $sth->execute() or die $sth->errstr;
>
> ? Or doesn't it matter?
>


It doesn't matter. errstr is global to the DBI package, so any valid object
should be able to access its value. In this case:

DBI->errstr;
$dbh->errstr;
$sth->errstr;

could all be used to print an error executing the statement. It's probably
not wise to use the statement handle itself, though, because it's the most
likely candidate not to have a valid object.

Matt


 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-19-2005
Matt Garrish wrote:
> my $dbh = DBI->connect('DBI:mysql:database', 'user', 'pass', { RaiseError =>
> 1, AutoCommit => 0 } )
> or die "Could not connect to database: " . DBI->errstr;


The 'or die ...' is redundant. You're setting RaiseError to 1, so as
soon as DBI->connect() fails to connect, the program will terminate,
before even processing the second part of the 'or' statement.

Paul Lalli

 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      10-19-2005

"Paul Lalli" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Matt Garrish wrote:
>> my $dbh = DBI->connect('DBI:mysql:database', 'user', 'pass', { RaiseError
>> =>
>> 1, AutoCommit => 0 } )
>> or die "Could not connect to database: " . DBI->errstr;

>
> The 'or die ...' is redundant. You're setting RaiseError to 1, so as
> soon as DBI->connect() fails to connect, the program will terminate,
> before even processing the second part of the 'or' statement.
>


Depends on your definitiion of redundancy. In most production code I'll turn
RaiseError off, but there are still instances where I want the messages. I
find it's easier to live with the redundancy than add the die statements
later.

Matt


 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      10-19-2005
Matt Garrish wrote:
> Gunnar Hjalmarsson wrote:
>>Matt Garrish wrote:
>>>
>>>$sth->execute() or die DBI->errstr;

>>
>>Shouldn't that be
>>
>> $sth->execute() or die $sth->errstr;
>>
>>? Or doesn't it matter?

>
> It doesn't matter. errstr is global to the DBI package, so any valid object
> should be able to access its value. In this case:
>
> DBI->errstr;
> $dbh->errstr;
> $sth->errstr;
>
> could all be used to print an error executing the statement.


Okay, thanks.

> It's probably not wise to use the statement handle itself, though,
> because it's the most likely candidate not to have a valid object.


Don't follow you there, Matt. If $sth isn't a valid object reference,
the errstr method won't tell you why $sth->execute() failed, will it?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
No Class at ALL!!! beginner/beginner question =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 7 02-03-2005 02:47 PM
DBI Mysql storing DBI:binary, trouble with character 26 Jerome Hauss Ruby 0 10-13-2004 03:04 PM
DBI and DBI::Oracle packages configuration ulloa Perl 1 07-22-2004 05:52 PM
DBI problem : How can I load quickly one huge table with DBI ??. Tim Haynes Perl Misc 3 09-13-2003 03:43 AM
Mason, DBI, and DBI::Pg Asby Perl Misc 0 07-24-2003 09:04 PM



Advertisments