Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-06-2002, 09:40 PM   PM User | #1
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
extract() and globals

How do I use extract() on an array but also make sure all the variables become global? Here's what I got

PHP Code:
<?php

include("/includes/db_connect.php");

$sql "SELECT * FROM config";
$result mysql_result($sql) or die("Query Failed ".mysql_error(););
$fetch mysql_fetch_array($result);
extract($fetch,EXTR_SKIP);
?>
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.
beetle is offline   Reply With Quote
Old 08-07-2002, 09:50 AM   PM User | #2
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
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

PHP Code:
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.
mordred is offline   Reply With Quote
Old 08-07-2002, 02:28 PM   PM User | #3
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
I see, so when I use extract from outside a function, the are already in the global namespace. Thanks mordred!
beetle is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:29 AM.


Advertisement
Log in to turn off these ads.