PDA

View Full Version : getting column names and field values help plz.


bazz
02-13-2008, 02:42 AM
Hi,

I am trying to select values from two tables and the column names from one of the tables. I have been able to do each select separately but I need your guidance and help to make one query do both jobs.

select column names


my $sth = $dbh->prepare ("SHOW COLUMNS FROM tbl_address FROM centralDb;
") or die "prepare statement failed: $DBO::errstr\n";



selecting the field values


my $sth = $dbh->prepare ("SELECT COLUMNS, BD.contact_id, BD.group_name, BD.premises_name, CNT.Contact_ID, CNT.Phone_Number, CNT.Fax_Number, CNT.Web_URL, CNT.email_Address
FROM tbl_businessDetails BD, tbl_contact CNT
WHERE BD.group_name = '$selected_group_name'
AND BD.premises_name = '$selected_premises_name'
AND BD.contact_id = CNT.Contact_ID
") or die "prepare statement failed: $DBO::errstr\n";


$sth->execute;



How can I make a single select, get me both lots of data?

bazz

Fumigator
02-13-2008, 08:41 AM
Can't do it; the first query is a SHOW, not a SELECT, therefore it doesn't retrieve data and it can't be joined/unioned with a SELECT statement that does retrieve data.

oesxyl
02-13-2008, 01:26 PM
Can't do it; the first query is a SHOW, not a SELECT, therefore it doesn't retrieve data and it can't be joined/unioned with a SELECT statement that does retrieve data.

I guess that Bazz want only to fecth the field name and the values in the same time, something like that:


while(my $row = $sth->fetchrow_hashref){
while(my ($fieldname, $value) each %$row){
print $fieldname,"\t",$value,"\n";
}
}


there are two problems with that:
- you must take care to avoid duplicate field name when you wrte the query expresion, last value overwrite previous
- you can't predict the order of fields in a hash

best regards

bazz
02-15-2008, 02:26 PM
Thanks oesxyl but, that doesn't seem to work.

I shall post in the perl forum as this is becoming more a perl question now.
bazz