Jump to content

assigning db field values to variables programmatically


geraldmpiper

Recommended Posts

This is what I'm doing:

 

Each time I create a new table in MySQL, I create new .php files to display the contents of the table, a form to add new records, a form to edit existing records, and a 'commit' file to commit my changes, either add or edit, to the table.  I often steal my old code, and then search & replace field names for the new table.  I find myself doing this over and over and over again.  And then, when I add a new field (or two or three) to a table, I must edit each of those files, and add new '$fieldName = $row['fieldName']' lines in different places.  This I also do over and over and over again.

 

What I would like to do:

 

I'd like to create an array of field names, and programmatically create the required variables (same names as the field names) and assign the field values to them.  Then, I'm hoping, it would just be a matter of adding the new field names to the array.

 

Am I crazy?  Or am I missing something?  Is there a good tutorial out there that can walk me through this?

 

I'm spending way too much time on the mechanics of coding correctly, and not enough time on what I want to do with my data.  I know there's a better way - but I'm having trouble finding it.

 

Thanks for your help.

Link to comment
Share on other sites

I don't see why it's not possible.  You can use functions like extract() and array_map(), or just foreach loops.

 

What I do is I write functions which do a particular task with the database, which may involve several tables, but which acts as a single unit.  I don't make interfaces for each table, as I rarely use any table in more than one or two different ways.  I work at a different level of abstraction.

Link to comment
Share on other sites

Hmm.. this ought to work:

 

$res = mysql_query(...);
while ($row = mysql_fetch_assoc($res)) {
  extract($row, EXTR_OVERWRITE);
}

 

But that will only work for a single row.. Actually, I don't really understand what you want :)  Since using $fieldName = $row['fieldName'] will only ever work with a single row at one time.  I'm not sure where you want the data to go after that..

 

The method above means you don't need to make any record of which columns are in the table.  But there is the question of how those variables get back to the calling function.  Usually I return an array like this:

 

$arr = array(
  'fieldName' => $fieldVal,
  'fieldName2' => $fieldVal2,
);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.