views:

46

answers:

1

I'm in a web scripting class, and honestly and unfortunately, it has come second to my networking and design and analysis classes. Because of this I find I encounter problems that may be mundane but can't find the solution to it easily.

I am writing a CGI form that is supposed to work with a MySQL DB. I can insert and delete into the DB just fine. My problem comes when querying the DB.

My code compiles fine and I don't get errors when trying to "display" the info in the DB through the browser but the data and text doesn't in fact display. The code in question is here:

print br, 'test';

my $dbh = DBI->connect("DBI:mysql:austinc4", "*******", "*******", {RaiseError => 1} );
my $usersstatement = "select * from users";
my $projstatment = "select * from projects";

# Get the handle
my $userinfo = $dbh->query($usersstatement);
my $projinfo = $dbh->query($projstatement);

# Fetch rows
while (@userrow = $userinfo->fetchrow()) {
    print $userrow[0], br;
}

print 'end';

This code is in an if statement that is surrounded by the print header, start_html, form, /form, end_html. I was just trying to debug and find out what was happening and printed the statements test and end. It prints out test but doesn't print out end. It also doesn't print out the data in my DB, which happens to come before I print out end.

What I believe I am doing is:

  • Connecting to my DB
  • Forming a string the contains the command/request to the DB
  • Getting a handle for my query I perform on the DB
  • Fetching a row from my handle
  • Printing the first field in the row I fetched from my table

But I don't see why my data wouldn't print out as well as the end text. I looked in DB and it does in fact contain data in the DB and the table that I am trying to get data from.

This one has got me stumped, so I appreciate any help. Thanks again. =)

Solution:

I was using a that wasn't supported by the modules I was including. This leads me to another question. How can I detect errors like this? My program does in fact compile correctly and the webpage doesn't "break". Aside from me double checking that all the methods I do use are valid, do I just see something like text not being displayed and assume that an error like this occurred?

+1  A: 

Upon reading the comments, the reason your program is broken is because query() does not execute an SQL query. Therefore you are probably calling an undefined subroutine unless this is a wrapper you have defined elsewhere.

Here is my original posting of helpful hints, which still apply:

  1. I hope you have use CGI, use DBI, etc... and use CGI::Carp and use strict;
  2. Look in /var/log/apache2/access.log or error.log for the bugs
  3. Realize that the first thing a CGI script prints MUST be a valid header or the web server and browser become unhappy and often nothing else displays.
  4. Because of #3 print the header first BEFORE you do anything, especially before you connect to the database where the script may die or print something else because otherwise the errors or other messages will be emitted before the header.
  5. If you still don't see an error go back to #2.
  6. CGIs that use CGI.pm can be run from a command line in a terminal session without going through the webserver. This is also a good way to debug.
Paul