X-Original-To: pgsql-php-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id E200DD1B16B for ; Sun, 16 May 2004 23:56:24 -0300 (ADT) 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 11028-06 for ; Sun, 16 May 2004 23:56:03 -0300 (ADT) Received: from lamb.mcmillan.net.nz (leibniz.catalyst.net.nz [202.49.159.7]) by svr1.postgresql.org (Postfix) with ESMTP id 5EF26D1B22D for ; Sun, 16 May 2004 23:55:42 -0300 (ADT) Received: from lamb.mcmillan.net.nz (lamb.mcmillan.net.nz [127.0.0.1]) by lamb.mcmillan.net.nz (Postfix) with ESMTP id 0C36EACB2809; Mon, 17 May 2004 14:55:33 +1200 (NZST) Subject: Re: table column information From: Andrew McMillan To: ljb , webid@cfl.rr.com Cc: pgsql-php@postgresql.org In-Reply-To: References: <1084749506.2056.148.camel@lathe> Content-Type: text/plain Message-Id: <1084762532.4822.36.camel@lamb.mcmillan.net.nz> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.5.7 Date: Mon, 17 May 2004 14:55:32 +1200 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200405/28 X-Sequence-Number: 2747 On Mon, 2004-05-17 at 00:19 +0000, ljb wrote: > > > > I am writing some php scripts where I want to generate a list of the > > column names in a particular table that the user selects. I could take > > the brute force method and hard code the column names but then every > > time I add a new table or modify an existing one I would have to modify > > the code. What I want is to have a generic function that given the > > table name it will pull the column names for my use. > > > > I need to get the table column names for several tables I have setup. I > > know if I do a select * from tablename I can then use the pg_fieldname > > function to pull the column names for all columns. > > > > But I don't think I want to select the entire contents of the table > > every time I want to get the names of the columns. I know this will > > work but I think performance will be very poor. > >... > > You almost got it - just do "select * from tablename where 0=1", which returns > no rows but will give you the fieldnames. A portable and (I think) > efficient way to get table column names. It can be a cute trick, and I use it myself from time to time (especially for "CREATE TABLE AS SELECT ..." where I want an empty table with the same structure, pre v 7.4 which can do this anyway). You should be aware however that as written above it will almost invariably force a full-table scan! You can also select the column names from the database metadata directly: SELECT attname FROM pg_class c join pg_attribute a on c.oid = a.attrelid WHERE c.relname = '' AND a.attnum >= 0; This approach won't get killed by the efficiency problems above. Cheers, Andrew. ------------------------------------------------------------------------- Andrew @ Catalyst .Net .NZ Ltd, PO Box 11-053, Manners St, Wellington WEB: http://catalyst.net.nz/ PHYS: Level 2, 150-154 Willis St DDI: +64(4)916-7201 MOB: +64(21)635-694 OFFICE: +64(4)499-2267 You have many friends and very few living enemies. -------------------------------------------------------------------------