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. Link to comment https://forums.phpfreaks.com/topic/72669-solved-assign-each-value-from-mysql-query-automatically/ 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; } ?> Link to comment https://forums.phpfreaks.com/topic/72669-solved-assign-each-value-from-mysql-query-automatically/#findComment-366391 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. Link to comment https://forums.phpfreaks.com/topic/72669-solved-assign-each-value-from-mysql-query-automatically/#findComment-366394 Share on other sites More sharing options...
scooter41 Posted October 10, 2007 Author Share Posted October 10, 2007 perfect thanks guys! Link to comment https://forums.phpfreaks.com/topic/72669-solved-assign-each-value-from-mysql-query-automatically/#findComment-366405 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 ?> Link to comment https://forums.phpfreaks.com/topic/72669-solved-assign-each-value-from-mysql-query-automatically/#findComment-366409 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.