[ic] SQL Statements within Usertags

Kevin Walsh interchange-users@icdevgroup.org
Thu Jan 16 11:34:00 2003


Rich Doughty [rich@opusvl.com] wrote:
>
> Usertag is:
>     UserTag         testcase Interpolate  1
>     UserTag         testcase HasEndTag    1
>     UserTag         testcase Routine      <<EOR
>         sub {
>             my ($body) = @_;
>             my $rv;
>     
>             my $rs = $Db {products}->query ('SELECT * FROM products');
>     
>             return 1;
>         }
>     EOR
>
> page contains:
>     [testcase] test [/testcase]
>
> Results are:
>     Apache internal Server error - Premature end of script headers
>
>     IC log shows "Runtime error: Can't call method "query" on an
>     undefined value at (eval 840) line 5."
>
Try this sort of thing instead (untested):

    UserTag testcase Interpolate 1
    UserTag testcase hasEndTag 1
    UserTag testcase Routine <<EOR
    sub {
        my $body = shift;     # unused in here

        my $db = ::database_exists_ref('products') or die 'No products table';
        my $query = q{
            SELECT  foo, bar
            FROM    products
        };
        my $resultset = $db->query($query) or die 'SELECT on products failed';

        foreach (@$resultset){
            # $_->[0] is foo
            # $_->[1] is bar
        }
        undef;
    }
    EOR

-- 
   _/   _/  _/_/_/_/  _/    _/  _/_/_/  _/    _/
  _/_/_/   _/_/      _/    _/    _/    _/_/  _/   K e v i n   W a l s h
 _/ _/    _/          _/ _/     _/    _/  _/_/    kevin@cursor.biz
_/   _/  _/_/_/_/      _/    _/_/_/  _/    _/