Re: [PHP] Database abstraction?




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/


.



Relevant Pages

  • Re: [PHP] Database abstraction?
    ... where I want to take out the field names in a database table and display them... ... So I need to know how to get the field names from the database so that I can populate $FIELDNAMES. ... then I can use my SELECT $FIELDNAMES FROM $table order by $order query to do my query... ... echo <<<HTML ...
    (php.general)
  • Form not working properly after adding new table columns to DB
    ... I've added about 19 fields to the database, and after modifying the ... // begin the query ... // end fieldnames and begin values ...
    (alt.php)
  • Re: DBMS and lisp, etc.
    ... Naively implemented with SQL, again for 10 ... (1 query for the initial orders, 1 query for each order for its ... soon as you upgrade to the SQL database. ... (eq (order-customer orderA) ...
    (comp.lang.lisp)
  • Re: OT: SQL
    ... query processing. ... FROM Employees e, Employees m, Management mgt ... Manager and Employee Salaries. ... The scheme used does not model database files in general, ...
    (sci.logic)
  • Re: access 2003
    ... I removed the parameters from the form query source. ... boxes from the form header, events, code, etc and ran the form query source ... forms queries and the SQL because syntax of the SQL will change randomly. ... the Access 97 database, I wouldn't have thought any expressions would be ...
    (microsoft.public.access.conversion)