View Full Version : extract() and globals

08-06-2002, 10:40 PM
How do I use extract() on an array but also make sure all the variables become global? Here's what I got



$sql = "SELECT * FROM config";
$result = mysql_result($sql) or die("Query Failed ".mysql_error(););
$fetch = mysql_fetch_array($result);
?>So basically I've extracted the results from MySQL into the symbol table, but how to I make sure each is global? I was going to skip using extract() and use a foreach() loop instead...but I'm not sure how to get it all done right.

08-07-2002, 10:50 AM
I don't understand your question quite, your code does extract the variables to the current symbol table, which is in your case the global namespace. Things would be different when you call extract() in a function. Is this what you want, creating global variables by using extract() in a function definition?

If so, you try to assign new fields to the $GLOBALS array in a foreach loop as you proposed... sth like

foreach ($fetch as $key = $val) {
$GLOBALS[$key] = $val;

Because you only need the associative keys of the array, I would recommend fetching the result with mysql_fetch_assoc(), but that's neglectible.

08-07-2002, 03:28 PM
I see, so when I use extract from outside a function, the are already in the global namespace. Thanks mordred!