1

I have the blow code saved in database

<script type="text/javascript" src="$url/js/jquery-ui.min.js"></script>
<script type="text/javascript" src="$url/js/ui.selectmenu.js"></script>

I want to fetch this code from database and then assign it to a variable and then I want to use $url which is defined above in my code in file.

So here is the scenario

$url is defined in file example.php foreg $url = 'http://localhost';

fetch a piece of code from database (the code above)

assign it to a variable foreg $content = (code fetch from mysql is here)

and in the end $content should contain

<script type="text/javascript" src="http://localhost/js/jquery-ui.min.js"></script>
<script type="text/javascript" src="http://localhost/js/ui.selectmenu.js"></script>

This code is just an example code and I have totally different code but scenario is kinda same. Anyone can please help.

P.S And my code could contain url() (a custom function I created in another file.)

5
  • 2
    What have you tried? it's echoing out a string..Or are you talking about evaluating php code stored in database, as your title says? Feb 19, 2012 at 12:00
  • Yes actually I want to evaluate my code by using eval() type of function. Unfortunately eval is not working. I have never used eval before but just heard of it Feb 19, 2012 at 12:06
  • If you need to programmatically rely on eval() to build your page, there's something wrong in how you're doing things. Are you sure there's no other way? Did you considered a templating system?(dwoo,twig,smarty) Feb 19, 2012 at 12:16
  • Let me tell you the scenario. I have a list of widgets. Only 1 widget can be enable at one time. I am using templatic wordpress theme and it needs to assign everything to $content in end to display certain things on certain area of page. At first I just put my code in file and assign it to the $content(the normal php way). But now my client needs to edit that code through wordpress admin panel. I thought of creating an admin option by saving that code in database and editing it. I am successfull in fetching the code and assigning it to $content but how ever I am not able to evaluate that code. Feb 19, 2012 at 12:31
  • The code is of widgets which I created my self. It contain php and html.. Every widget has different code and I save that code in database. The html is code works good but the php is evaluating Feb 19, 2012 at 12:31

2 Answers 2

2
$content = str_replace('$url', 'http://localhost', $content);
1
  • No as I said its just an example code.. My code contain many variables like $url which defined across several files. so I need to use some kind of eval() functionality. and unfortunately eval is not working Feb 19, 2012 at 12:04
2

You could do something like this:

$result = mysql_query("SELECT code FROM MyCodeTable WHERE MyColumn = 'something'");

if ($result && mysql_num_rows($result) > 0) {
    $row = mysql_fetch_object($result);

    $content = str_replace('$url', $url, $row->code);
}

Notes: Ideally you wouldn't fetch your data from MySQL like this, but use another method like PDO instead. Also, like Seagull illustrated in his example, using str_replace is a safer way to do this rather than eval because you should not trust the data you store in your database.

4
  • But What if my code contain 45 different variables like $url ? Feb 19, 2012 at 12:14
  • This example assumes that you are storing markup in your database (which is a bad thing to do, by the way). If you need to replace many things, then yes, it could become a tedious task, even though str_replace allows you to pass in arrays. It would surely be easier to use eval if this is the case.
    – ba0708
    Feb 19, 2012 at 12:22
  • If you are trying to use eval on all of the code, then I think that is your problem. It sounds as if eval is returning FALSE due to a parse error. I haven't actually used eval myself, but you will find an example on how to mix HTML and PHP with eval in the PHP Manual (see the parameters section). Also, I would suggest that you do not store the markup in the database unless you have a very good reason to. Either just store script names and paths, for instance, or use files for this.
    – ba0708
    Feb 19, 2012 at 16:56
  • Thanks andy124 for the help :) Feb 20, 2012 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.