Re: Database abstraction?
| From: | Chris | Date: | Wed, 16 Apr 2008 21:37:21 +0000 |
| Subject: | Re: Database abstraction? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-273128@lists.php.net to get a copy of this message | ||
Jason Pruim wrote:
Hi Everyone! I'm back with yet another question.... But getting closer to sounding like I know what I'm talking about and that's all thanks to all of you. A free beer (Or beverage of choice)* for everyone who has helped me over the years! Here's my question... I have a program, where I want to take out the field names in a database table and display them... In other words instead of writing: $Query ="SELECT First, Last, Middle, Add1 FROM mytable order by $order"; I want to write something more like: $Query ="SELECT $FIELDNAMES FROM mytable order by $order"; So I need to know how to get the field names from the database so that I can populate $FIELDNAMES. I played a little bit and it looks like I might be able to get what I want by doing 2 queries... $QueryFieldNames = "DESCRIBE $table"; And then some sort of a foreach or maybe a while to populate $FIELDNAMES? Maybe an array so I could do $FIELDNAMES['First'] if I needed to later. then I can use my SELECT $FIELDNAMES FROM $table order by $order query to do my query...If $FIELDNAMES contains all the fields, I have to ask why? If you just want them for the header or something you could do something like this: $fields = array(); $query = "select * from $table order by $order"; $result = mysql_query($query); $row_counter = 0; while ($row = mysql_fetch_assoc($result)) { if ($row_counter == 0) {
$fields = array_keys($row);} print_r($row); } Haven't actually tested it but saves another round trip to the db. -- Postgresql & php tutorials http://www.designmagick.com/