Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I see, so when I use extract from outside a function, the are already in the global namespace. Thanks mordred!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •