Jump to content

Trying to get sql result array into variables


geradt

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']

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.