geradt Posted January 10, 2011 Share Posted January 10, 2011 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/223983-trying-to-get-sql-result-array-into-variables/ Share on other sites More sharing options...
Maq Posted January 10, 2011 Share Posted January 10, 2011 Can we see your current code? Quote Link to comment https://forums.phpfreaks.com/topic/223983-trying-to-get-sql-result-array-into-variables/#findComment-1157483 Share on other sites More sharing options...
geradt Posted January 10, 2011 Author Share Posted January 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/223983-trying-to-get-sql-result-array-into-variables/#findComment-1157486 Share on other sites More sharing options...
AbraCadaver Posted January 10, 2011 Share Posted January 10, 2011 Something like: while($clientData2 = mysql_fetch_array($result2, MYSQL_ASSOC)) { ${$clientData2['fld_id']} = $clientData2['fld_vl']; } Quote Link to comment https://forums.phpfreaks.com/topic/223983-trying-to-get-sql-result-array-into-variables/#findComment-1157498 Share on other sites More sharing options...
geradt Posted January 10, 2011 Author Share Posted January 10, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/223983-trying-to-get-sql-result-array-into-variables/#findComment-1157517 Share on other sites More sharing options...
AbraCadaver Posted January 10, 2011 Share Posted January 10, 2011 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'] Quote Link to comment https://forums.phpfreaks.com/topic/223983-trying-to-get-sql-result-array-into-variables/#findComment-1157523 Share on other sites More sharing options...
geradt Posted January 10, 2011 Author Share Posted January 10, 2011 Thanks Abra. Much appreciated. That did the trick. I appreciate your time and effort! Have a great day. Quote Link to comment https://forums.phpfreaks.com/topic/223983-trying-to-get-sql-result-array-into-variables/#findComment-1157559 Share on other sites More sharing options...
geradt Posted January 10, 2011 Author Share Posted January 10, 2011 One more thing.... For some reason, its not creating a variable for the first row in the results? It starts on the second row? any idea why? do I need to do anything else to get it to returnt the first row? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/223983-trying-to-get-sql-result-array-into-variables/#findComment-1157602 Share on other sites More sharing options...
AbraCadaver Posted January 10, 2011 Share Posted January 10, 2011 Get rid of the original: //Loads the user data into an array $clientData2 = mysql_fetch_array($result2, MYSQL_ASSOC); Quote Link to comment https://forums.phpfreaks.com/topic/223983-trying-to-get-sql-result-array-into-variables/#findComment-1157610 Share on other sites More sharing options...
geradt Posted January 10, 2011 Author Share Posted January 10, 2011 That did the trick. Thanks again for your help. Quote Link to comment https://forums.phpfreaks.com/topic/223983-trying-to-get-sql-result-array-into-variables/#findComment-1157613 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.