scooter41 Posted October 10, 2007 Share Posted October 10, 2007 Hi There... is there anyway after retrieving a mysql query, I can automatically assign "all values with a respective variable name"? Example: instead of saying $name=$row['name']; $email=$row['email]; $username=$row['username']; that I can just say ... retrieve data query... then for all row fields, assign to a respective variable name? I'm moving from a perl/cgi land which it was relatively simple there, so assume it is here too Thanks for any help in advance. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted October 10, 2007 Share Posted October 10, 2007 Sure. Try: <?php foreach($row as $k => $v){ $$k = $v; } ?> Quote Link to comment Share on other sites More sharing options...
Orio Posted October 10, 2007 Share Posted October 10, 2007 That should do the work: <?php foreach($row as $k => $v) $$k = $v; ?> Orio. Quote Link to comment Share on other sites More sharing options...
scooter41 Posted October 10, 2007 Author Share Posted October 10, 2007 perfect thanks guys! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted October 10, 2007 Share Posted October 10, 2007 No problem. Just fyi, the use of the double dollar sign means you are using a variable variable: the name of the variable you assign $v to is the value of the variable $k. Perhaps another example illustrates that better: <?php $foo = 'bar'; $bar = 'test'; echo $$foo; //is equivilant to echo $bar, since 'bar' is the value of $foo //produces test ?> Quote Link to comment 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.