php, pg_field_name, columns missing
Eric Chadbourne
eric.chadbourne-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Sun Nov 21 12:44:13 EST 2010
Hi All,
I'm trying to generate a report in CSV format. I desire the column
names in the first row. Oddly I'm outputing only the first few column
names. Anybody in the mood to take a peak at my chicken scratch and
point out a mistake? Many thanks.
<code>
// do query
$v_all_members_0 =3D "select * from members_50congress where
gym_name =3D '$v_gym_name';";
$v_all_members_1 =3D pg_query($dbconn, $v_all_members_0) or
die('Query failed: ' . pg_last_error());
$rows =3D pg_num_rows($v_all_members_1); // count up rows in que=
ry
$rows =3D $rows -1; // avoids an index not found error
// write file
$report_file_name =3D 'output_am_'.date("YmdHi").'.csv';
$fp =3D fopen($report_file_name, 'w');
=09
// get column names and write to file
// TODO: why the frack isn't this printing all column names?
$i =3D 0;
while ($i <=3D $rows) {
fwrite($fp, pg_field_name($v_all_members_1,$i));
$i++;
=09
if ($i <=3D $rows) {
fwrite($fp, ',');
} else {
fwrite($fp, "\n");
}
=09
}
=09
// loop through rows and write to file
$i =3D 0;
while ($i <=3D $rows) {
fputcsv($fp, pg_fetch_array($v_all_members_1,$i,PGSQL_NUM));
$i++;
}
fclose($fp);
</code>
Many thanks!
Eric C
More information about the Discuss
mailing list