Jump to content

[SOLVED] Set Variable name use Field name from table


jvrothjr

Recommended Posts

I have a query string that sets the value of Variables below:

 

if ($DB == "Departments") {
$tracking_id = $data22['tracking_id'];
$dept_name = $data22['dept_name'];
}

 

simple and straight forward.

 

Now I would like to set a loop that would set the variable name and value based on the field names in the table queried.

 

if ($DB == "Departments") {
$querystring = "Select * from ".$DB;
$result=mysql_query($querystring);
while(($field = mysql_fetch_field($result))) {
	$field->name = $data22[$field->name];
}
}

 

can I set the variable name using the $field->name if so how?

 

I have tried this code with no luck and no luck searching.

 

This is a small example. I have queries with fifteen plus fields.

 

This would also remove the need to modify this code if a field needs to be added to the table.

 

And yes I have had to add fields to the tables.

Link to comment
Share on other sites

  • 3 months later...

try

if ($DB == "Departments") {
   $querystring = "Select * from ".$DB;
   $result=mysql_query($querystring);
   while(($field = mysql_fetch_field($result))) {
      $$field->name = $data22[$field->name];
   }
}

Link to comment
Share on other sites

This works for me:

 

while($fld = mysql_fetch_field($result))

    {

 

$field_name=($fld->name); //assigns field name to a variable

 

//makes a new variable with the name of the field ie a field called John makes a variable called $john     

        ${"{$field_name}"} = $_POST[$fld->name];

 

}

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.