http://qs321.pair.com?node_id=469378


in reply to Re: Perl and DBI and CGI
in thread Perl and DBI and CGI

Dear monkey_boy ! Thank you, Much better, but I have no idea how to improve formating and assign fields to the variables: $Name and $Email. In other words, how to make
print "<font color=\"red\"> $Name"; print "<font color=\"ble\"> $Email";
Regards, sOKOle

Replies are listed 'Best First'.
Re^3: Perl and DBI and CGI
by monkey_boy (Priest) on Jun 23, 2005 at 13:26 UTC
    You would be better specifying the column names in the sql, thus would be able to help you more, also dont forget to close your font tags , something like this:
    my $ref = $dbh->selectall_arrayref(" select name , email , age from users ") || die $dbh->errstr; foreach my $person (@{$ref}) { my ($name,$email,$age) = @{$person}; print "<font color=\"red\" > $name </font>"; print "<font color=\"blue\"> $email </font>"; };



    This is not a Signature...
      monkey_boy, your script works, but main problem re-occured When I have "Dr. John Bull" field it prints "Dr." only... could you solve ?
        Im finding it hard to help as you dont provide data for us!
        What does this print?

        use Data::Dumper; my $ref = $dbh->selectall_arrayref("select * from users") || die $dbh->errstr; print Dumper($ref);



        This is not a Signature...
Re^3: Perl and DBI and CGI
by sapnac (Beadle) on Jun 23, 2005 at 13:27 UTC
    Hi! Check if this is helps...
    $query = "SELECT Name, Email FROM users"; $sth = $dbh->prepare($query); $sth->execute(); while($Name,$Email) = $sth->fetchrow_array){ print "$Name, $Email";} $sth->finish(); $dbh->disconnect;
      Hi,

      The code is right, but using arrayref is more efficient/faster, consider it if you have a lot of data.

      Regards,
      ;-)
      Sorry, it does not help.
        I am really sorry that it is not working for you. Actually, I created a temptable and tried executing the code that you are using;
        my $ref = $dbh->selectall_arrayref(" select name, email from testtable") || die $dbh->errstr; foreach my $person (@{$ref}) { my ($name,$emai) = @{$person}; print "<font color=\"red\" >Name : $name </font>"; print "<font color=\"blue\"> Email : $email </font><br>"; + }
        It runs just fine even with the "Dr. John Bull". Please check the database if some special characters are not slipped thru.
        here is the output
        Name : Dr. John Bull Email :
Re^3: Perl and DBI and CGI
by blazar (Canon) on Jun 23, 2005 at 13:23 UTC
    Hint: use alternate delimiters, e.g. qq|"$whatever"|.