Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2004
    Posts
    5

    Unanswered: How to display query results in the same webpage

    Ok, newbie question here. I can do DBI queries in Perl and I can make web pages, but how in the world to I create an html page that invokes DBI queries that will show in the same web page? Can someone point me in the right direction?

    Thanks!

    - WJB

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Some assists:
    * use the CGI module
    * do all querys
    * print site


    for example
    Code:
    #! /usr/bin/perl
    
    use strict; # to avoid misspellings, declare all variables with 'my'
    use warnings;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser); # to get the error messages on to the browser
    use DBI; # to work with the database
    
    my $cgi = CGI->new(); # create an new instance of CGI
    print $cgi->header(-type => 'text/html'); # a header is required
    
    my $statement = 'SELECT * FROM table;';
    
    print $cgi->h1('My first Webpage with Perl and MySQL'); # print a header
    print "<b>subtitle</b>"; # print a bold subtitle
    
    my $user = 'dbuser';
    my $password = 'dbpass';
    my $database = 'db';
    my $host = 'localhost';
    my $dbh = DBI->connect("DBI:mysql:$database:$host",$user,$password) or die $!;
    my $sth = $dbh->prepare($statement);
    $sth->execute();
    while(my @cols = $sth->fetchrow_array()){
      print "@cols"; # display it in the browser
    }
    $sth->finish();
    $dbh->disconnect();
    
    # next query
    # next print
    HTH
    reneeb
    board.perl-community.de - The German Perl-Community

Posting Permissions

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