fetching unkown column names

Reply

Join Date: Apr 2008
Posts: 35
Reputation: cali_dotcom is an unknown quantity at this point 
Solved Threads: 0
cali_dotcom cali_dotcom is offline Offline
Light Poster

fetching unkown column names

 
0
  #1
Nov 18th, 2008
i'm building a website where users can add articles. on the admin side, they can also add new fields on the add articles page.
to allow the user edit the articles, i have to query the database and then assign the results from the query to variables, usually i use the $fieldname.
the problem is, if a user added a new field while he added the article, i can't fetch the content by field anme from the rusult set of the query.
eg:
  1. $select = $article->select();
  2. $select->where('id =?', $id);
  3. while ($rows = $article->fetchAll($select)){
  4. $this->cat_id = $rows->cat_id;
  5. $this->article_title = $rows->title;
  6. $this->description = $rows->description;
  7. :
  8. :
  9. }
but if i dont know the name of the new field(s) the user added, i can't assign it to a variable.
PS: i was thinking of a solution that would need me to fetch the name of the fields form the article_fields table, but then again i got stuck trying to assign the field name to a variable that had the same name i.e:

  1. $select = $article_fields->select();
  2. while ($rows = $article_fields->fetchAll($select)){
  3. ${$rows->field_name}= $rows->field_name;
  4. ;

Does anyone have any solutions??
Last edited by cali_dotcom; Nov 18th, 2008 at 9:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 232
Reputation: Rhyan is an unknown quantity at this point 
Solved Threads: 24
Rhyan's Avatar
Rhyan Rhyan is offline Offline
Posting Whiz in Training

Re: fetching unkown column names

 
0
  #2
Nov 18th, 2008
Your select can always be ' select * from whatever'.
Then you just retrieve an associative array using mysql_fetch_assoc, and then run a foreach loop like this

  1. foreach( $result as $key => $value)
  2. {
  3. echo 'Field name:'.$key."\n Field value".$value;
  4. }
Last edited by Rhyan; Nov 18th, 2008 at 9:04 pm.
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC