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.
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?
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.
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.
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?
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");
}
----------------------------------
#!/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 :-)
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?
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;
}
}
}
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
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?