$sql = "SELECT * FROM params" $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr); $sth->execute() or die("Could not execute!" . $dbh->errstr); while (($param_id, $param_name) = $sth->fetchrow_array()) { $param_names{$param_id} = $param_name; } $sth->finish; $sql = "SELECT * FROM objects" $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr); $sth->execute() or die("Could not execute!" . $dbh->errstr); while (($object_id, $param_id, $param_value) = $sth->fetchrow_array()) { $objects{$object_id}{$param_id} = $param_value; } $sth->finish; #print column headers foreach $param_id (sort keys %param_names) { print $param_names{$param_id}.", "; } #print data foreach $object_id (sort keys %objects) { foreach $param_id (sort keys %param_names) { print $objects{$object_id}{$param_id}.", "; } }