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
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)."\";");

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.