in reply to Creating a csv from two database tables
$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}.", "; } }
Needs cleanup of trailing commas.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Creating a csv from two database tables
by joec_ (Scribe) on Apr 07, 2009 at 08:47 UTC |