tags:

views:

119

answers:

8

Is there any possibility to write php code to mysql and then use it in php, in order to process the output, not just write it?

I would like to use mysql, instead of included file...if it is possible.

+2  A: 

eval

hsz
@hsz: The `eval` is **evil** !
Sarfraz
I know. But he asks about it. It is just an answer.
hsz
-1 for suggesting eval()
Marco Mariani
@Marco Mariani: So suggest something *better*, before you mod people down.
FrustratedWithFormsDesigner
Yes. You usually code something that takes parameters from the DB, but don't use the DB as a poor man's VCS. I don't think the OP has some deep metaprogramming issue that cannot be solved with conventional methods, but we'll never know if he doesn't provide details.
Marco Mariani
+1  A: 

For the love of ${DEITY}, no!!!

Justin K
+6  A: 

You can use the eval() function to run a string as PHP code.

Store the string in the database and then get it from a query and run eval.

As everyone here will be saying, this isn't the best practice. There's a good chance that there is a better solution that you just haven't thought of yet. If there isn't, make sure the values in the database are not user editable of there could be some serious problems!

Alternatively, if you want to play it safer, you could define the PHP functions that can be called, and just store the function name. Then use call_user_func() to run the function!

This is much safer since you have explicitly defined the functions available to be run, but less flexible of course.

Brendan Bullen
-1 for suggesting eval()
Marco Mariani
@Marco Care to suggest something else? There seems to be quite a consensus here...
Brendan Bullen
@Marco Mariani: Why -1? It does exactly what the OP wanted and is probably the easiest way to do it. Unless you have a better suggestion (do you?), don't mod down.
FrustratedWithFormsDesigner
Probably because eval() is **evil** :p.
wimvds
@FrustratedWithFormsDesigner (good name) You're exactly right. The function exists so it makes no sense to down-vote someone for mentioning it. Everyone that has suggested it has included a warning. I still can't see any alternative suggested by Marco
Brendan Bullen
Yes, eval is evil, *normally*. But occasionally, for example if you want to run compiled code without caching it may be unavoidable and shall not be avoided!
nikic
The right way to help the OP is understanding the problem. A quick and dirty fix that gives me some karma but creates even more entropy than PHP and MySQL already provide, is not my idea of help.
Marco Mariani
@Marco Or, you could treat the OP as an adult and allow them to choose which approach they will use. Sufficient warning on its use was given. The OP has asked a question and it has been answered. Somehow, I think PHP and MySQL will survive.
Brendan Bullen
Actually, I did revert the downvote when you provided a warning. But the OP never told us his problem - he asked how to implement a (probably very wrong) solution to a problem we don't know yet.
Marco Mariani
That is a fair point. But at the end of the day, the answer to the question was correct. Thank you for reverting the downvote and, for the record, I do think eval is a sin against nature. My hands felt dirty after typing it.
Brendan Bullen
+2  A: 

Sure you can, you may use eval. But beware - eval is evil and if it contains user input a malicious user may take over your server. So, please, be kind, and don't use eval!

nikic
+1  A: 

As others have answered, yes, that is possible; you can use eval() to run arbitrary PHP code. But it is rarely if ever a good idea to store PHP in the database and eval() it.

Perhaps you could outline what exactly you want to achieve and why you feel storing PHP in the database is a good solution. That way, if anyone feels he has a better solution for your problem, he can suggest it.

Hammerite
+2  A: 

Yes you can store the PHP code like any other text and then use eval() to run it.

But: You won't get any warnings/errors if your code is wrong, only on runtime. This makes debugging your code extremely difficult.

So don't do it!
Really, I am serious about this. In the end, you will have a lot more work.

Besides that, without this database thing, your code is also easier to read and understand by others. They don't need to know what code is in the database, they can just look up the file that is included.

Felix Kling
A: 

Sure it is possible and can be a good speed improvement. I'm using the cms "contenido" (www.contenido.org) where this practice is used for some editable components, (layouts/modules/data types etc.). Finally all code for one article is stored in one field and is performed with one eval call. Contenido is not perfect, nevertheless a good example for this practice.

Missing version control is a disadvantage. But it is not a real problem with a simple way to export and import the code fragments.

giftnuss
A: 

As everyone says it's a bad practice to store your code in database and use eval function. My personal reasons are:

  • I care about debugging (there are plugins for php that let you debug your php code. PHP debugger is integrated in PhpEd and I believe it is integrated in NetBeans too) and it would be more difficult with eval.

  • Performance issues:

The speed of eval Besides security concerns eval also has the problem of being incredibly slow. In my testing on PHP 4.3.10 its 10 times slower then normal code and 28 times slower on PHP 5.1 beta1. (Source: http://blog.joshuaeichorn.com/archives/2005/08/01/using-eval-in-php/)

EDIT: Here are my results from testing script above on my machine (PHP 5.3.0):

Eval: 1000000 times took 8.2261250019073n

Same code not eval: 1000000 times took 0.27089691162109n

Eval in function: 1000000 times took 0.8873131275177n

So "evaled" code is 30.4 times slower than not evaled version of the same code.

MartyIX
That article's from 2005 and compares against a beta release. Is there a more current benchmark (not that I think eval will ever have great performance, just wondering if there is more current info)?
FrustratedWithFormsDesigner
I ran the script on my machine and added my results. I didn't find any other newer benchmark of eval.
MartyIX