electricshoe Posted October 3, 2007 Share Posted October 3, 2007 Heya I'm trying to set a dynamic variable as a global in a function. It's a simple function that takes a $result from a mysql query and gets the array with a custom variable name. function queryData($data) { $GLOBALS[$data] = mysql_fetch_array($GLOBALS['result']); } queryData("customRowName"); echo $customRowName['id']; This code doesn't work. The query is fine, I can do print_r($GLOBALS[$data]) and it returns my array. I've tried $GLOBALS[${$data}] and it doesn't work either. I'm getting a notice that the variable is unset for both of them Notice: Undefined variable: customRowName in ---- on line 20 Any help would be greatly appreciated, thanks:) Quote Link to comment https://forums.phpfreaks.com/topic/71702-solved-dynamic-variables-in-globals/ Share on other sites More sharing options...
MadTechie Posted October 3, 2007 Share Posted October 3, 2007 $GLOBALS[$data] is an ARRAY so you need to treat it like one.. ie foreach($GLOBALS[$data] as $ndata) { echo $ndata; } edit: as a note for the ID use $GLOBALS[$data]['id'] Quote Link to comment https://forums.phpfreaks.com/topic/71702-solved-dynamic-variables-in-globals/#findComment-360964 Share on other sites More sharing options...
electricshoe Posted October 3, 2007 Author Share Posted October 3, 2007 You're beautiful. $GLOBALS['customRowName']['id'] loves me and I'm a noob with for statements so I have been educated. If I were a girl and hot I would kiss you Quote Link to comment https://forums.phpfreaks.com/topic/71702-solved-dynamic-variables-in-globals/#findComment-360979 Share on other sites More sharing options...
MadTechie Posted October 3, 2007 Share Posted October 3, 2007 LOL, i'll settle for you clicking solved (bottom left) Quote Link to comment https://forums.phpfreaks.com/topic/71702-solved-dynamic-variables-in-globals/#findComment-360985 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.