Forum Moderators: coopster

Message Too Old, No Replies

Can I execute PHP code from a MySQL variable?

         

gargoyle1

9:24 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



I have a few lines like this:


if (something){
print str_replace($array_values, $array_variables, $section_code);
}
else {
do something else}

everything fetches and prints from the DBase just fine until I have something like 'echo $somestring;' in the retrieved code.

instead of printing 'whatever the string is' it prints 'echo whatever the string is'

do I need to replace the php commands somehow?

jatar_k

9:28 pm on Jun 20, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



do you want to replace them or have them parsed?

you could use
[php.net...]

lobo235

9:29 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



use the eval() function to evaluate the string as PHP code.

gargoyle1

9:43 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Ultimately, I want the code that is in the MySQL field to be executed on the current page, variables and all.

Until this point, I was simply replacing variables because I didn't need to use code.

I have a situation now that is easily solved if I can store some PHP with variables inside the code, and let it execute at certain times.

I basically need one statement that execute the whole thing, variables, and code when needed. If I can execute the whole thing as code without having to worry about replacing the variables, thats fine too.

gargoyle1

10:07 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



the eval() function isn't working properly, which is why I opted to use the str_replace.

Let me rephrase that.. I'M having trouble using it properly. I'm sure it's working just fine ;)

jatar_k

10:34 pm on Jun 20, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$somestring = 'whatever the string is';
$content = 'echo $somestring;';
eval($content);

gargoyle1

11:02 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



I've been messing with it for about half an hour now rewriting things where necesary. It's working intermittently, but I'm confident that I can make it work totally.

I've been using eval($somestring);

How different is that from the method that you suggested where I echo $somestring into $content, and eval ($content)?

jatar_k

11:05 pm on Jun 20, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



mine is just an example since I am not going to build a table, store some code in a col and then select and eval it ;)

so i threw some php code into a var and eval'ed the var, that's all I wanted to show you.

You just eval whatever the var is where you have the code selected from the column in your db.

gargoyle1

11:19 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Cool, I seem to have figured it out with your help. Thanks a million for your prompt responses.

I think that the problem here is that I'm building a script that is so complex that when I look at it from too wide an angle, I have no idea wth I'm doing. LOL

10 dollar brain building 100 dollar site. ;)