Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl/MySQL/CGI -> hangs

Status
Not open for further replies.

Coderifous

Programmer
Dec 13, 2001
782
US
Hey all,

I'm developing Perl-Powered websites w/ backend DB's in MySQL. I use DBI.pm. For admin use, I have a script that dumps the contents of a MySQL table, into an HTML table to the browser. This works 99% of the time without a hitch. I mean - it takes a minute, but thousands of lines will dump to the browser if that's what I tell it to do. No problem there.
Here's the problem:
I wrote a script to do this routine 'dump' on another routine table. As you can see, there is nothing special so far. However, when I execute the script via browser - it hangs. It 'prints' everything up to the beginning of the table and then stops. (So I can see the page title, header, and that's it). After waiting any arbitrary amount of time (it can be 2 seconds or 10 minutes - doesn't matter) I'll click the 'stop' button on my browser, and that's when (using MS Internet Explorer) the available tables contents are displayed. It's not the whole table though. The HTML stops at the same point everytime. Like for some reason MySQL just stops talking right there, and the rest of the processes are just sitting there waiting - wondering.
I don't know if I was very clear on this, but the bottom line is, MySQL is causing my script to hang and it shouldn't. Has anyone else heard of/dealt with this behavior? Would enjoy a little assistance. And oh yea, here is some of my code (I'll abrreviate some peices):


Code:
...

my $query = "SELECT * FROM access_log ORDER BY date";
my $dbh = WebDB::connect('access');  #personal module
my $sth = $dbh->prepare($query);
$sth->execute();

print <<EOF;
<!-- Some HTML here that starts the table -->
EOF

while(my @results = $sth->fetchrow_array()){
   local $\ = &quot;\n&quot;;
   print &quot;<TR>&quot;;
   shift @results;  # toss the id field,
                    # don't need it here
   print &quot;<TD>$_</TD>&quot; for @results;
   print &quot;</TR>&quot;;
}

print &quot;</TABLE>&quot;;

...

Keep in mind that, syntactically, everything is fine. I don't think it's Apache since other similar scripts run just fine (and return larger and smaller datasets). I really think it's MySQL.

Thanks - I hope.

--jim
 
Don't know how, but I fixed it:
OLD Loop:
Code:
...
while(my @results = $sth->fetchrow_array()){

   #this was the problem!!!!?!?!?!
   my $color=($color eq $WebDB::color_1)?
      $WebDB::color_2 : $WebDB::color_1;
   

   local $\ = &quot;\n&quot;;
   print &quot;<TR>&quot;;
   shift @results;  # toss the id field,
                    # don't need it here
   print &quot;<TD>$_</TD>&quot; for @results;
   print &quot;</TR>&quot;;
}

print &quot;</TABLE>&quot;;

...
New Loop:
Code:
...

while(my @results = $sth->fetchrow_array()){
   
   #take away the my() declaration and I'm good to go.
   $color=($color eq $WebDB::color_1)?
   $WebDB::color_2 : $WebDB::color_1;
   

   local $\ = &quot;\n&quot;;
   print &quot;<TR>&quot;;
   shift @results;  # toss the id field,
                    # don't need it here
   print &quot;<TD>$_</TD>&quot; for @results;
   print &quot;</TR>&quot;;
}

print &quot;</TABLE>&quot;;

...

Can someone else try this out and see if it hangs for them too? I sure would like to know if we've found a perl bug.

--jim
 
I don't have WebDB installed so can't help you there.

That problem/solution seems pretty strange - I don't understand that at all. If you figure it all out, please post again. Hardy Merrill
 
Oops. Sorry about that Hardy:
My WebDB.pm module is just a couple shortcuts for my self. Here is the non-WebDB revised snippet:

Code:
# $dbh is the common name for a database handle using
# DBI.pm  So connect to a database you have available
# to you, and use $dbh as your DBI object.

# replace db_name with a table name that you have 
# in your database.
my $query = &quot;SELECT * FROM db_name&quot;;

my $sth = $dbh->prepare($query);
$sth->execute();


print &quot;<TABLE>&quot;;

while(my @results = $sth->fetchrow_array()){
   my $color = ($color eq '&quot;220022&quot;') ? 
      '&quot;110011&quot;' : '&quot;220022&quot;';
   local $\ = &quot;\n&quot;;
   print &quot;<TR>&quot;;
   shift @results;  # toss the id field,
                    # don't need it here
   print &quot;<TD BGCOLOR=$color >$_</TD>&quot; for @results;
   print &quot;</TR>&quot;;
}

print &quot;</TABLE>&quot;;

OK - now it should doable. I really would like to know if it bugs out on your systems too.

Once again - my fix for the problem was merely removing the my() declaration from the variable $color. Now in retrospect - that was the wrong place to have it anyways -- becuase it was going out of scope everytime it looped - and there by defeating what I was attempting. But it still shouldn't hang like that.

--jim
 
I didn't try your code, but something similar happened to me just a couple of days ago. I was testing a script (no mySQL, it just used flatfiles) both on my local machine and on a remote server. They had different perl versions. On my computer, it kept timing out at the same time during a certain operation. When I manually killed perl on my local machine, it displayed a little bit of what it was supposed to, but barely any. The info displayed was also the same each time.

I eventually changed something that was completely irrelevant - I don't even remember what it was - and somehow, it began to work fine again.
 
Jim, sorry it took me so long to get to this - I'm working on a new installation and I had to get MySQL up and running again with users/privs/db's/etc.

I ran your code - but I had the &quot;-w&quot; switch on my top line

#!/usr/bin/perl -w

and I got this error when I tried to run it:

------------------------------------------------------
Global symbol &quot;$color&quot; requires explicit package name at ./select_customer.pl line 31.
Execution of ./select_customer.pl aborted due to compilation errors.
-------------------------------------------------------

Here is the code section I copied in:

while(my @results = $sth->fetchrow_array()){
my $color = ($color eq '&quot;220022&quot;') ? ### this is line 31
'&quot;110011&quot;' : '&quot;220022&quot;';
local $\ = &quot;\n&quot;;
print &quot;<TR>&quot;;
shift @results; # toss the id field,
# don't need it here
print &quot;<TD BGCOLOR=$color >$_</TD>&quot; for @results;
print &quot;</TR>&quot;;
}

So it looks like the $color in ($color eq '&quot;220022&quot;') needs to be defined first. If I define 'my $color = &quot;&quot;;' before the while loop, it actually works and I get output.

I'm running on Red Hat Linux 7.2 on perl 5.6.1
* DBI 1.18
* DBD::mysql 2.0416

HTH. Hardy Merrill
 
Thanks Hardy, I really appreciate your help on this. I wonder why it's saying that $color is a global??? Declaring it with my() is explicitly saying &quot;NOT A GLOBAL, NOT EVEN A PACKAGE GLOBAL&quot;.

Have any insight as to what the deal is there? It seems that for the very first time in my experience, that using the -w warings pragma actually would've saved me time.

I just may start using it. Maybe.

--jim
 
I don't really have a good explanation, but I guess I can &quot;sort of&quot; understand it. When you declare with &quot;my&quot; the variable that will catch the assignment, that is kind of separate from the expression on the right of the equals sign. I could be wrong about this, but don't assignment statements(or expressions) get evaluated right-to-left? If that is true, then the &quot;my&quot; declaration may not be seen until the end of that statement getting evaluated - that could be why you're getting the error. I'm really not sure about this.

I have to disagree on how useful -w is - the biggest thing that I can think of that it does for me is prevent me from misspelling variable names(by making you declare every variable you're going to use, and giving an error on any variable reference not already defined). I know that when not using -w in the past, I've lost a lot of time going around and around trying to figure out what the problem is, only to find out that I misspelled a variable name. I know it does lots of other stuff too ;-) Hardy Merrill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top