Forum Moderators: coopster & phranque

Message Too Old, No Replies

can't locate object method via package DBI

         

warrior01

6:55 pm on Sep 13, 2004 (gmt 0)

10+ Year Member



hi there,

I'm just a newbie and am trying to get my first perl script to work. But no success yet.

The code below gives this error..

Can't locate object method "connect" via package "DBI" at show.cgi line 5

Here's the code:
----------------------
#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);

$dbh = DBI->connect("DBI:mysql:workatho_links:localhost","workatho_workath","lifesucks");

my $sth = $dbh->prepare( "SELECT * FROM links" );
$sth->execute() or die "Could not Execute: $DBI::errstr";

$sth->finish();

$dbh->disconnect();
----------------------

I tried quite a bit myself to see if it would help any. Such as, I added "use DBI;" (without the quotes) at the beginning of the script. I added the s, w and t flag after the path to perl and all didn't help either.

When I added use DBI; to the script it gave me the "500 internal server" error.

Anyways, is there anyone out there that can help me out? All I need is a peice of code that I know "works" and then I'll fool around with it after that. That's the fastest way for me to learn. I just can't seem to learn from books and online tutorials :(

Thanks in advance.

UDaMan

7:17 pm on Sep 13, 2004 (gmt 0)

10+ Year Member



use DBI; # This is correct

When I added use DBI; to the script it gave me the "500 internal server" error.

"DBI:mysql:workatho_links:localhost"

Have you tried :-

====================================
"DBI:mysql:workatho_links;localhost" # Note the semi-colon
====================================

And finally do you have the DBI module installed?

moltar

7:23 pm on Sep 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You always need to "use DBI;".

The reason you were getting 500 error when you added the above line, was probably, because you do not have DBI module installed. If it is a fresh Perl install, then you do not have that module, it does not come with perl dist.

You will need to install DBI and DBD::mysql

On windows you can use program called ppm that comes with perl and is in the bin directory.

cyberws

9:13 pm on Sep 13, 2004 (gmt 0)

10+ Year Member



I would need to see the error log but if the DBI/DBD isn't install and you have access to install modules the following should install it.

At the command prompt type:

cpan (hit enter)

Once the cpan engine is loaded type:

install DBI (hit enter)

Once complete type:

install DBD::MySQL (hit enter)

That should install the DBI/DBD if it isn't installed and you have the permission.

warrior01

10:10 pm on Sep 13, 2004 (gmt 0)

10+ Year Member



I tried the code at my new webhost, not sure if they have that DBI module installed.

So I also tried the code at my old webhost who do have the DBI module installed.
I even contacted my webhost to see if they knew why my script won't work. But a tech replied saying they're a little swamped today so basically I'm on my own for now. The only hint they gave me was to add the -s to my path to perl which I already tried and didn't help any.

But all I need is a piece of code that "works". Is there someone out there who could rewrite my code? I'm thinking that a perl expert could do this with his eyes closed :-)
Just need a itty bitty piece of code that I can play around with. I'll try out stuff and if it goes wrong than I will always have a backup file of the code that works and then I'll simply start over. But I don't have any code yet that works.... a little help please?

cyberws

10:19 pm on Sep 13, 2004 (gmt 0)

10+ Year Member



Below is a script that works. Assuming you setup the connection variables correctly and the server has the necessary modules install.

1) Copy and paste into a file and name it something like dbtest.cgi
2) Upload it and chmod to 755 and execute from browser. If you get a successful printed the server supports it.

You can also try executing from shell if you have access.

shell>perl /full/path/to/dbtest.cgi
=========================CODE==============
#!/usr/bin/perl

$mysqldatabase="database";
$mysqlusername="user";
$mysqlpassword="pass";

print "Content-type: text/html\n\n";
&connect;

print "Successful";
exit;

sub connect {
use DBI;
$dbh = DBI->connect("dbi:mysql:$mysqldatabase","$mysqlusername","$mysqlpassword") ¦¦ die("Couldn't connect to database!\n");
}

warrior01

1:08 am on Sep 14, 2004 (gmt 0)

10+ Year Member



thanks for the script but it didn't work. I keep getting the internal server error.

I checked the error log and it said

"Premature end of script headers"

Any idea how I fix that?

cyberws

2:19 pm on Sep 14, 2004 (gmt 0)

10+ Year Member



Hmm... that is a very basic script.

What line is the error occuring on? What do your logs say? When you run the script manually are you getting the same error? If Apache isn't setup correctly I have seen problems.

warrior01

4:19 pm on Sep 14, 2004 (gmt 0)

10+ Year Member



I fooled around with that code you gave me, and here is what eventually worked..

----------------------------------
#!/usr/bin/perl -s

use CGI::Carp qw(fatalsToBrowser);

$db="<databasename>";
$un="<username>";
$ps="<password>";

print "Content-type: text/html\n\n";
&connect;

print "Successful";
exit;

sub connect {
use DBI;
$dbh = DBI->connect("dbi:mysql:$db","$un","$ps") ¦¦ die("Could not connect to database \n");
}
----------------------------------

When I first tried your script I got a internal server error. Then I added the -s after the path to perl and then when I tried it again, I got a unrecognized character at line .. error (forgot what line :)

I was thinking maybe it was the ' in the word couldn't so I removed it but it still didn't work.

Then I added the line

use CGI::Carp qw(fatalsToBrowser);

and did some other stuff that probably wouldn't have made a difference anyway (like I changed $databasename to $db) but somehow it worked. Yeah, maybe you're as surprised as I am :-)

And a few hours later I managed to get the following code to work as well

---------------------------------
#!/usr/bin/perl -s

use DBI qw(:sql_types);
use CGI::Carp qw(fatalsToBrowser);

print "Content-type: text/html\n\n";

my $dbh = DBI->connect("DBI:mysql:<databasename>;localhost","<username>","<password>");

my $sth = $dbh->prepare("SELECT * FROM links");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
print "<p><a href=$ref->{'url'}>$ref->{'title'}</a><br>$ref->{'desc'}\n";
}

$sth->finish();
$dbh->disconnect();
---------------------------------

Of course I replaced <databasename> <username> and <password>, with the right info.

Now I'm working on inserting stuff into the table but no luck yet. Will get there though. You better believe it :-)

cyberws

5:04 pm on Sep 14, 2004 (gmt 0)

10+ Year Member



Strange. That code is very basic and has worked on many systems, however each system is slightly different and the important thing is you modified it to work.

Do you need the SQL calls to insert, delete and such or are you good to go there?

warrior01

6:18 pm on Sep 14, 2004 (gmt 0)

10+ Year Member



I now know how to SELECT stuff from the database. Now I'm trying to find out how to insert data from an html form into the database. But I'm not sure how I retrieve that data.

For example <input type="text" name="url">

What is the perl code to get the url that is entered in that field? Like this?

$param('url')

I have a table called links and I have three fields called title, desc and url.

Below is the code that I thought would work but didn't. It inserted something allright, but it inserted the names title, desc and url. These are the names of the fields, it didn't insert what I entered in these fields. So appearently $param('url') is wrong. What am I supposed to use here?

-------------------------------
#!/usr/bin/perl -s

use DBI qw(:sql_types);
use CGI::Carp qw(fatalsToBrowser);

print "Content-type: text/html\n\n";

my $dbh = DBI->connect("DBI:mysql:<databasename>;localhost","<username>","<password>");

$dbh->do("INSERT INTO links VALUES ($param('title'), $param('desc'), $param('url'))");

print "<script language='javascript'>
location.replace('http://www.business-opportunity-guide.com/text.html')
</script>";

$dbh->disconnect();
-------------------------------

But other than that, am I using valid code? Am I learning the right way so far?

cyberws

7:07 pm on Sep 14, 2004 (gmt 0)

10+ Year Member



In programming there are many ways to accomplish the same thing. Below is just how I would do it, although there are a few other ways to code it.

When inserting into a table it is better to list the order of the columns before the actual data. This helps avoid data corruption.

====================================
#!/usr/bin/perl

&connect;
&parse_form;

$query = "insert into tablename (title,desc,url) values (\"$FORM{'title'}\",\"$FORM{'desc'}\",\"$FORM{'url'}\")";
$dbh->do($query);

print "Content-type: text/html\n\n";
print "page code";
exit;

sub connect {
use DBI;
$dbh = DBI->connect("dbi:mysql:$mysqldatabase:localhost","$mysqlusername","$mysqlpassword") ¦¦ die("Couldn't connect to database!\n");
}

sub parse_form {

my ($pair, @pairs);
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
# Split the name-value pairs
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
# Get the input
my $buffer;
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);
}
else {
&error('request_method');
}

foreach $pair (@pairs) {
my ($name, $value) = split(/=/, $pair);

$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$value =~ s/<!--(.¦\n)*-->//g;

# Put form data into variables
if ($FORM{$name} && ($value ne '')) {
$FORM{$name} = "$FORM{$name}, $value";
}
elsif ($value ne '') {
$FORM{$name} = $value;
}
}
}

warrior01

7:29 pm on Sep 14, 2004 (gmt 0)

10+ Year Member



I tried your code.

First I got a 500 error. So I added this line

use CGI::Carp qw(fatalsToBrowser);

Then I got a unrecognized character on line 17, which were these ¦¦ which I turned into these ¦¦

Then I tried the code again and it said "page code" on a blank page. I thought it worked but nothing was inserted into the database though.

Will play with the code a little more :-)

Oh and my path to perl appearently requires the -s

cyberws

7:49 pm on Sep 14, 2004 (gmt 0)

10+ Year Member



I would recommend using an auto_increment field to count up on your table and is possible /usr/local/bin/perl might work a little better. On some systems they point to different versions of Perl but I can't speak for the server setup you are using.

warrior01

11:32 pm on Sep 14, 2004 (gmt 0)

10+ Year Member



my values keep dissappearing.

This is what I have..

values (\"$FORM{'title'}\",\"$FORM{'desc'}\",\"$FORM{'url'}\")";

and this is what's left of it when I reopen the file after I ran the script..

values (\"\")";

Is this normal? how can a script run a second time when chunks of code just dissappears like that?

cyberws

8:22 pm on Sep 15, 2004 (gmt 0)

10+ Year Member



That isn't normal. Nothing should disappear.