> 
>       I posted something like this a week ago, but typos in my message kept
> anyone from understanding the issue.
> 
>       I am trying to return each row to the client as it comes from the
> database, instead of waiting for all the rows to be returned before
> displaying them.  

Why would you want to do this ?

Writing your application this way will ensure that:

a. end users can crash your server/application.
b. your application will preform poorly on the network.

Buffer your output, and when all the output is collected, print it, and
let tcp deliver the data in network-friendly chunks. If your database is
that slow that you think you need an approach like this, investigate the
possibility of a caching server process that you can sit in front of the
actual db. 

You need to consider what happens when a user executes a query that can
return more rows that a browser can reasonably display. In other words,
having a query results pagination module or feature is probably a must.

If you were writing a stand-alone application that ran on a single cpu
(like MS Access on a local file) in  this style (no pagination, no
buffering) I would consider this to be marginally bad style. Inside a
web-based application, this approach is horrendous.

Just my 2 cents . . .

On Sat, 23 Dec 2000, quagly wrote:

> 
>       I have set $|=1 and added $r->flush; after every print statement ( I
> realize this is redundant ) but to no avail.  
> 
> This is the relevant code:
> 
> while ($sth->fetch) {
>    $r->print ("<TR>",
>        map("<TD>$_</TD>",@cols),
>        "</TR>");
>   $r->rflush;
> }
> 
> Here is the complete package:
> 
> package Sql::Client;
> 
> use Apache::Request;
> use strict;
> use warnings;
> use Apache::Constants qw(:common);
> 
> my $r;                                      #request
> my $apr;                                   #Apache::Request 
> my $host;                                  #hostname of remote user
> my $sql;                                    #sql to execute
> 
> $|=1;
> 
> sub getarray ($) {                                                     
> 
>         my $dbh;              # Database handle
>         my $sth;                # Statement handle
>         my $p_sql;             # sql statement passed as parameter
>         my @cols;              #column array to bind results
>         my $titles;               # array ref to column headers
> 
>         $p_sql = shift;
> 
>         # Connect
>         $dbh = DBI->connect (
>                 "DBI:mysql:links_db::localhost",
>                 "nobody",
>                 "somebody",
>                 {
>                         PrintError => 1,            # warn() on errors
>                         RaiseError => 0,           # don't die on error
>                         AutoCommit => 1,        # commit executes
> immediately
>                 }
>         );
> 
>         # prepare statment
>         $sth = $dbh->prepare($p_sql);
> 
>         $sth->execute;
> 
>         $titles = $sth->{NAME_uc};
>         #--------------
>         # for minimal memory use, do it this way
>         @cols[0..$#$titles] = ();
>         $sth->bind_columns(\(@cols));
>         $r->print( "<TABLE BORDER>");
>         $r->print ("<TR>",
>                 map("<TD>$_</TD>",@$titles),
>                                                 "</TR>");
>         while ($sth->fetch) {
>                 $r->print ("<TR>",
>                         map("<TD>$_</TD>",@cols),
>                                                         "</TR>");
>                 $r->rflush;
>         }
>         $r->print ("</TABLE>");
>         return; 
> }
> 
> 
> sub handler {
>         $r = shift;
>         $apr =  Apache::Request->new($r);
>         $sql = $apr->param('sql') || 'SELECT';
>         $sql='SELECT' if  $apr->param('reset');
> 
>         $r->content_type( 'text/html' );
>         $r->send_http_header;
>         return OK if $r->header_only;
>         $host = $r->get_remote_host;
>         $r->print(<<HTMLEND);
> <HTML>
> <HEAD>
> <LINK REL="stylesheet" TYPE="text/css" 
>     HREF="/styles/lightstyle.css" 
> >
> <TITLE>Hello $host</TITLE>
> <BODY>
> <H1>Sql Client</H1>
> <FORM METHOD="POST">
> <P>Enter your Select Statement:
> <BR>
> <TEXTAREA  NAME="sql" ROWS=8 COLS=60 WRAP>$sql</TEXTAREA>
> <BR>
> <INPUT TYPE="SUBMIT" VALUE="Submit">
> <INPUT TYPE="SUBMIT" NAME="reset" VALUE="Reset">
> </FORM>
> HTMLEND
>         $r->rflush;
>         getarray($sql) unless $sql =~ /^SELECT$/;
> 
>         $r->print(<<HTMLEND);
> </BODY>
> </HTML>
> HTMLEND
>         return OK;
> }
> 1;
> 

Reply via email to