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

sOKOle has asked for the wisdom of the Perl Monks concerning the following question:

My best friends, please help me to improve this:
The Part of CGI Code is:
$query = "SELECT * FROM users"; $sth = $dbh->prepare($query); $sth->execute(); $sth->bind_columns(undef, \$Name, \$Email); while($sth->fetch()){ print "$Name, $Email";} $sth->finish(); $dbh->disconnect;erl
The Problem and Question are:
When the Name in my Data Base is "Dr. John Bull" only "Dr." is printed. How to improve the code to have full name printed?
Thank you in advance,
Sincerely yours,
sOKOle

Replies are listed 'Best First'.
Re: Perl and DBI and CGI
by monkey_boy (Priest) on Jun 23, 2005 at 13:00 UTC
    I agree with blazar on both points, but try this & see if it sheds some light:
    use Data::Dumper; my $ref = $dbh->selectall_arrayref("select * from users") || die $dbh->errstr; print Dumper($ref);



    This is not a Signature...
      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
        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...
        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;
        Hint: use alternate delimiters, e.g. qq|"$whatever"|.
Re: Perl and DBI and CGI
by blazar (Canon) on Jun 23, 2005 at 12:53 UTC
    At the risk of being downvoted, two mostly OT and probably irrelevant for you, cmts:
    1. This seems to be more of a DB question than an actual Perl one; i.e. I don't think things are different when you directly access the DB as opposed to interface with it with perl.
    2. You have quite a strange indenting style, and an IMHO confusing one. Maybe giving a peek into perlstyle would help.
    As far as the first point goes, I have only extremely limited experience with this stuff myself, but there may be preferable approaches to interfacing with DBses. Here at work they're all enthusiastic with Class::DBI.