X-Original-To: pgsql-php-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 54486D1B437 for ; Wed, 26 Nov 2003 19:52:18 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 01087-09 for ; Wed, 26 Nov 2003 15:51:54 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id 6365DD1B469 for ; Wed, 26 Nov 2003 15:51:47 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id hAQJpZNw021254 for ; Wed, 26 Nov 2003 19:51:36 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id hAQJVOrb017758 for pgsql-php@postgresql.org; Wed, 26 Nov 2003 19:31:24 GMT From: "Tom Hebbron" X-Newsgroups: comp.databases.postgresql.interfaces.php Subject: Re: Saving result to file for download Date: Wed, 26 Nov 2003 20:31:20 +0100 Organization: Hub.Org Networking Services Lines: 117 Message-ID: References: <200310272056.28937.skoot@qi.pl> X-Complaints-To: usenet@news.hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 To: pgsql-php@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200311/53 X-Sequence-Number: 2446 This script initially displays a form for you to enter connection details, a query, and a return type. The form then POSTs these details to itself. When the script detects that form data is being POSTed to it, it connects to the PG databasew specified in the POST data, and runs the query. The query is then rendered into the desired output format, and the correct headers are sent to the browser to save the file. Note - I don't seem to be getting consistent behaviour with the fle name disposition header - not sure if this is IIS or IE6 fouling up. ################################################################### \n\n"); for($i=0;$i\n"; foreach(pg_fetch_assoc($result,$i) AS $field=>$value) $output .= "\t\t$value\n"; $output .= "\t\n"; } $output .= "\n"; break; case "csv": header("Content-type: text/csv"); header("Content-Disposition: attachment; " . $output_filename . ".csv"); foreach(pg_fetch_assoc($result,0) AS $field=>$value) $output .= "\"$field\","; $output = rtrim($output,",") . "\n"; for($i=0;$i$value) $output .= "$value,"; $output = rtrim($output,",") . "\n"; } break; default: header("Content-type: text/plain"); header("Content-Disposition: attachment; " . $output_filename . ".txt"); foreach(pg_fetch_assoc($result,0) AS $field=>$value) $output .= "\"$field\"\t\t"; $output = rtrim($output,"\t") . "\n"; for($i=0;$i$value) $output .= "$value\t\t"; $output = rtrim($output,"\t") . "\n"; } break; } print($output); die(); } } ?> Downl query results demo
PostgreSQL server:
PostgreSQL database:
PostgreSQL port:
PostgreSQL user: password:
Query:
Output filename:
Output type: