Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl/MySQL problem

I'm not sure about an error message I'm getting.

         

Aardvark

3:34 pm on Oct 20, 2001 (gmt 0)



I'm attempting to write a Perl/MySQL topsites scirpt. So I'm running the script, everything is working except for a few minor bugs from typos.  Then I join.  Nope, doesn't work.  No data goes to the table.  I try several times, pressing the enter key harder each time.  No go.  After several hours of frustration, I find that there is this error if I execute it in telnet:

DBD::mysql::st execute failed: MySQL server has gone away at ./topsites.cgi line 463.

So I look at line 463, which is:

$sth->execute;

I don't see a problem there.  In fact, many parts of the script that work use the sql_do subroutine that that line is part of.  So now I'm confused.  The part of join.cgi that calls that subroutine looks like this:

     $SQL = "INSERT INTO aardvarktopsitesdata VALUES ('$at3{num}','$form{url}','$form{title}','$form{description}','$form{urlbanner}','$form{email}','$form{password}')";
     &sql_connect;
     &sql_do;
     $SQL = "INSERT INTO aardvarktopsitesstats VALUES ('$at3{num}','$totalranks','$totalranks','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0')";
     &sql_do;
     &sql_disconnect;

It should also be noted that the sql subroutines from topsites.cgi are as follows:

sub sql_connect {
 use DBI;
 $DSN = "DBI:$database:$db_name";
 $dbh = DBI->connect($DSN,$db_username,$db_password)
   ¦¦ die "Cannot connect: $DBI::errstr\n" unless $dbh;
 return;
}
sub sql_disconnect {
 $dbh->disconnect;
}
sub sql_do {
 eval{ $sth = $dbh->prepare($SQL); };
 if($@){
   &sql_disconnect;
   print "Content-type: text/html\n\n";
   print "An ERROR occurred! $@\n";
   exit;
 }
 else {
   $sth->execute;
 }
 return $sth;
}
sub sql_strip {
 $_[0] =~ s/\'/\\'/g;
 $_[0] =~ s/\"/\\"/g;
 return $_[0];
}

But here's the real stumper.  join.cgi is called by a require in topsites.cgi.  If I slightly edit join.cgi and add the above subroutines to the bottom of it, then CHMOD 755 and run, it works like a charm.  I will be extremely grateful and generous to whoever can help me with this.  It is the only major issue holding back my script. Thank you!

sugarkane

5:23 pm on Oct 20, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Aardvark, welcome to WebmasterWorld

It sounds like the script is somehow dropping the connection to MySQL early. Try removing this section to see if it's causing a problem:
[perl]
if($@){
&sql_disconnect;
print "Content-type: text/html\n\n";
print "An ERROR occurred! $@\n";
exit;
}
[/perl]

If that's not it, it might be a variable scoping issue - check that $DSN or $dbh are not being redefined somewhere else in the script.