Jump to content

Recommended Posts

Hi everyone,

 

I really need some help with a project I'm working on. I have a table in MySQL that I am querying to pull out some data.

 

This is the results I am getting from my query:

 

-----------------------------------------------

| fld_id              | fld_vl                    |

-----------------------------------------------

| CLNT_NM        | Motel1                  |

| CLNT_MGR      | Frank                    |

| CLNT_TMZ        | EST                      |

-----------------------------------------------

 

What I would like to do is get a variable for each fld_id and populate it with the field value.

 

So, in essence it would look like this.

 

$CLNT_NM = 'Motel1';

$CLNT_MGR = 'Frank';

$CLNT_TMZ = 'EST';

 

How can I do this? I have tried to figure this out but I can't seem to do it.

 

Any help you guys can provide would be greatly appreciated. THANKS in advance!!!

Here is my code:

 

$query2 = "SELECT fld_id, fld_vl FROM clnt_data 
$result2 = mysql_query($query2);
mysql_close();

/Loads the user data into an array
$clientData2 = mysql_fetch_array($result2, MYSQL_ASSOC);

foreach($clientData2 as $k=>$v) $$k = $v;

 

The foreach statement I used above puts the column name into the $k variable and then puts the actual values for fld_id and fld_vl into the $v variable.

 

Instead I want the fld_id value to be the variable name and then set that variable equal to the fld_vl.

 

I hope this makes sense.

Thanks Abra.

 

That worked. The only issue is that some of the values in the fld_it colum have a dash in it. So when I try to use the variable later in the code it errors because of the dash.

 

For example:

 

FLD_ID = MGR-NM  FLD_VL = FRANK

 

your code does what I want and makes the following:

 

$MGR-NM = 'FRANK';

 

But if I try to use that later on like this:

 

echo $MGR-NM;

 

it bombs becase it doesn't like the dash in the variable name.

 

I think the only way around that is to remove the dashes from the FLD_ID's.

 

Thanks for your help!

Thanks Abra.

 

That worked. The only issue is that some of the values in the fld_it colum have a dash in it. So when I try to use the variable later in the code it errors because of the dash.

 

For example:

 

FLD_ID = MGR-NM  FLD_VL = FRANK

 

your code does what I want and makes the following:

 

$MGR-NM = 'FRANK';

 

But if I try to use that later on like this:

 

echo $MGR-NM;

 

it bombs becase it doesn't like the dash in the variable name.

 

I think the only way around that is to remove the dashes from the FLD_ID's.

 

Thanks for your help!

 

Yes, or I would use an array in here:

 

while($clientData2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
   $data[{$clientData2['fld_id']}] = $clientData2['fld_vl'];
}

 

Then just use $data['MGR-NM']

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.