In DBI, it (fetchrow_hashref, not fetchhash) returns a hash where the
keys = column names and values = value of the columns on the row.  I
consider it a generaly bad thing since it forces you to alias your
columns when you have two (or more) columns with the same name or an
expersion as a column in you result set.

select a.id, 
       a.name, 
       a.create_date,
       b.purchase_amount,
       b.create_date,
       b.create_date - a.create_date
        from customer a, purchase b
        where a.id = b.id;

has column names of qw(id, name, create_date, purchase_amount,
create_date, expression).  When DBI assigns data to the hash the code
would look like this:

$hash{id}              = $row[0];
$hash{name}            = $row[1];
$hash{create_date}     = $row[2];
$hash{purchase_amount} = $row[3];
$hash{create_date}     = $row[4];
$hash{expression}      = $row[5];

as you can see we lose the first create_date since it is overwritten by
the second, and goddess help us if we have more than one expression.  To
fix this we must alias the column names:

select a.id                          as a_id, 
       a.name                        as a_name, 
       a.create_date                 as a_create_date,
       b.purchase_amount             as b_purchase_amount,
       b.create_date                 as b_create_date,
       b.create_date - a.create_date as interval_between
        from customer a, purchase b
        where a.id = b.id;

Frankly I prefer to work with the array ref I get back from
fetchrow_arrayref or the array I get back from fetchrow_array.

$sth->execute;
while (($id, $cust_name, $cust_create_date, $ammount, $bought_on,
$time_diff) = $sth->fetchrow_array) {
        do stuff
}
$sth->finish;


On 07 Jun 2001 16:34:48 -0400, Bradshaw, Brian wrote:
> You guys have been so nice.. I have another question :)
> 
> What does fetchhash do?
> 
> I have the code :
>       $return_hash = $dbh->query($query) or print "$query\n\n";
>       %result = $return_hash->fetchhash();
> But I am unsore of what the fetchhash does.
> 
> Thanks again!
> 
> Brian Bradshaw
> Systems Engineer
> McGraw-Hill Education
> McGraw-Hill Companies
> 860-409-2603
> [EMAIL PROTECTED]
> 
--
Today is Pungenday, the 12nd day of Confusion in the YOLD 3167
Kallisti!


Reply via email to