Re: Database abstraction?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux