On Apr 16, 2008, at 5:37 PM, Chris wrote:
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?
What I am trying to accomplish is a customer wants me to add custom
fields to their table in my database, I want to use the same code to
display the separate fields... In other words right now I have the
field names hard coded into my app.. I want to be able to remove the
actual field names and have them pulled dynamically from the database,
so it doesn't matter if there is 10 fields or 30 fields, it will print
a header row that contains ALL the field names and format the table
properly.
Does that help clarify?
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