SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
Thread: eval() an array from a database
-
Feb 17, 2004, 11:02 #1
eval() an array from a database
I have two fields in a database, one for a setting_name and one for a setting value. I then want to eval() the setting_name to make it a PHP variable and set it to the corresponding value. I've got it working fine for standard text values in setting_value but as soon as I put an array in it stops working. To test it I set up the following code
PHP Code:$property = "emailAddressesSetting";
$value = '"news" => "news@domain.com","administrator" => "admin@domain.com"';
eval("\$$property = array(\$value);");
foreach ($emailAddressesSetting as $key=>$value)
{
echo $key." - ".$value."<br />";
}
any help much appreciatedHEXUS Webmaster
-
Feb 17, 2004, 11:10 #2
- Join Date
- Sep 2003
- Location
- Wixom, Michigan
- Posts
- 591
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It seems to me your error is subtle yet sneaky.
Try instead of this
PHP Code:eval("\$$property = array(\$value);");
PHP Code:eval("\$$property = array($value);");
-
Feb 17, 2004, 21:22 #3
- Join Date
- Dec 2003
- Location
- Melbourne, Australia
- Posts
- 66
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I use serialize() and unserialize() to store an unknown length array in my database tables. Something like
INSERT INTO user SET preferences = '.serialize($prefs)
Cheers
Dave
-
Feb 18, 2004, 03:50 #4
jesus, i hate it when it turns out to be something simple
Your suggestion was spot on ghurtado. Dave thanks for your reply as well, I didn't get around to using it but I've noted it for future use as I haven't used the serialize function before
thanks againHEXUS Webmaster
-
Feb 18, 2004, 07:26 #5
- Join Date
- Feb 2004
- Location
- London, UK
- Posts
- 21
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes serialize is a much safer way to do this kind of thing.
Justin
----------------------------------------------------------------
http://php.jvmultimedia.com - PHP Freeware and Tutorials
-
Feb 19, 2004, 09:01 #6
- Join Date
- Sep 2003
- Location
- Wixom, Michigan
- Posts
- 591
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Glad I could help
Bookmarks