Jump to content

eval() with PHP Variables in Database


blastin311

Recommended Posts

Hello,

 

I am having an issue getting the eval() to work.

 

In my database I am storing a select box with options. I name the select box using a variable ($options):

 

<select id="required1-$id2" name="required1-$id2" disabled='disabled' style='margin-top:5px'>

<option>Choose Option</option></select>

 

I am trying to pull this from the database and still retain use of that variable. In my while loop I am grabbing the data from the database and putting it into a variable:

 

$options = $myrow["options"];

eval("\$options = \"$options\";");

 

I guess I don't understand how the eval() does its magic cause I keep getting Parse error: syntax error, unexpected T_STRING on the eval()'d code. Can anyone shine some light on this for me? Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/173041-eval-with-php-variables-in-database/
Share on other sites

when you substitute everything in...this is what is being eval'd:

$options = "<select id="required1-$id2" name="required1-$id2" disabled='disabled' style='margin-top:5px'>
<option>Choose Option</option></select>";

As you can see...your double quotes won't work....you can escape them though:

 

eval("\$options = \"".str_replace('"','\"',$options)."\";");

eval is a very expensive way of going about things - I can't really think of a situation where I would use it on purpose.  I'd always look to avoid it if possible...

agreed...expensive and dangerous...you may want to look into doing simple substitution instead with str_replace(). or, check out something like Smarty (PHP templating engine)

I appreciate the suggestions. I am storing a drop-down list of "Options" for a product in a database. They are being pulled for display because each one is either required or optional.

 

I don't think it will effect it much as it is a small app with little else to do than configure the the options to a product.

 

What do you think? Too expensive for such a small app or too expensive in general.

 

Would this work the same (or better)?

$options = str_replace('"$options"', '\"$options\"', $options);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.