Results 1 to 5 of 5

Thread: How could I view results from db in HTML page?

  1. #1
    Join Date
    Mar 2003
    Location
    Chennai, India
    Posts
    25

    How could I view results from db in HTML page?

    Hi all,

    I am querying the Mysql database with cgi-perl and want the output to be displayed in an HTML page? I am able to view the result but with the tags on when I compile. But I need to view the results in a browser. What should I do?

    What are the prerequisites if any, I need to take care of?

    Sorry if the question sounds stupid.
    Would someone help me with a solution?

    Thanks in advance.
    Karthikeyan

  2. #2
    Join Date
    Feb 2003
    Location
    Johannesburg, South Africa
    Posts
    145
    Hi - I am assuming you mean using Perl scripts in your cgi-bin directory and not mod_perl for Apache.

    I also assume you are getting the MySQL data.

    If I understand your problem correctly you just need to print out the response header in your CGI script. Before printing the HTML, add the following line:

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

    I hope that was your problem.

    Cheers

  3. #3
    Join Date
    Mar 2003
    Location
    Chennai, India
    Posts
    25

    Re: Query results in webpage:revisited

    Hi nicc77,

    I have the code sample_db.cgi in cgi-bin/ which is setup as ScriptAlias.

    When I give the path of the program in a web-browser,it shows the program as a junk.When I tried compiling at the location, it displays the results just in between the mentioned tags. The program is only ordinary perl and not mod_perl.

    Probably, this is because I am new to cgi-programming. Pls help me with some references for cgi-perl programming on the web.

    With best regards.
    Karthikeyan

  4. #4
    Join Date
    Mar 2003
    Location
    Chennai, India
    Posts
    25
    Hi nicc77,

    It worked. I gave /usr/bin/perl -w for the script beginning. But I have a feeling it should work otherwise too.

    It showed Internal Server error 500: What could be the reason? Any idea.

    Meanwhile, could I come know of your name plz.

    Regards
    Karthikeyan

  5. #5
    Join Date
    Feb 2003
    Location
    Johannesburg, South Africa
    Posts
    145
    Here is a sample Perl script. Call it printenv.pl:

    #!/usr/bin/perl
    ##
    ## printenv -- demo CGI program which just prints its environment
    ##

    print "Content-type: text/html\n\n";
    print "<html><body>";
    foreach $var (sort(keys(%ENV))) {
    $val = $ENV{$var};
    $val =~ s|\n|\\n|g;
    $val =~ s|"|\\"|g;
    print "${var}=\"${val}\"<br>\n";
    }
    print "</body></html>";
    After you have save it in /var/www/cgi-bin ( or wherever your ScriptAlias points to ) do the following ( as root ):

    # cd /var/www
    # chown -Rv apache.apache cgi-bin
    # chmod -Rv 770 cgi-bin
    # cd /var/www/cgi-bin
    Your printenv script should already have the permissions rwxrwx--- if you do a ls -l

    Check from the command line:

    # ./printenv
    Content-type: text/html

    <html><body>BROWSER="/usr/bin/mozilla"<br>
    COLORFGBG="default;default;0"<br>
    COLORTERM="rxvt-xpm"<br>
    DISPLAY=":0.0"<br>
    ENV="/root/.bashrc"<br>
    GDK_USE_XFT="1"<br>
    GDMSESSION="WindowMaker"<br>
    G_BROKEN_FILENAMES="1"<br>
    etc...
    And now try it in your browser: http://127.0.0.1/cgi-bin/printenv

    If you get errors, check your error log:

    # tail -10 /var/log/httpd/error_log
    If it says something like "premature end of ..." then there is probably a syntax error in your code that the -w option didn't pick up when you run the app. Probable candidates are:

    * Incorrect SQL syntax
    * DBI syntax errors ( calling fetcrow_array instead of fetchrow_array - 'h' is missing in the first one )

    Debugging can be painfull. What I do if I run into similar problems is to comment everything out that has to do with the SQL query. The SQL query is ussually in a variable $sql, and I then do a:
    print "<p>SQL=$sql</p>\n";
    After I get the output in the Browser, I run it in MySQL command line client, and make sure the output as is expected.

    Hope this could help somewhat. In terms of resources, I strongly recommend the following resources:

    * www.cgi101.com
    * Google

    Cheers

    PS: my name is simply Nico.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •