简体   繁体   English

保存在数据库中的PHP CODE需要运行

[英]PHP CODE Saved in database needs to be run

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. 我想从数据库中获取此代码,然后将其分配给一个变量,然后我要使用$ url,它在文件的代码中已在上面定义。

So here is the scenario 所以这是场景

$url is defined in file example.php foreg $url = 'http://localhost'; $ url在文件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) 将其分配给变量,例如$ content =(从mysql提取代码在此处)

and in the end $content should contain 最后$ content应该包含

<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. 任何人都可以帮忙。

PS And my code could contain url() (a custom function I created in another file.) PS并且我的代码可能包含url()(我在另一个文件中创建的自定义函数。)

$content = str_replace('$url', 'http://localhost', $content);

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. 注意:理想情况下,您不会像这样从MySQL获取数据,而是改用PDO之类的另一种方法。 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. 另外,就像Seagull在他的示例中说明的那样,使用str_replace而不是评估是一种更安全的方法,因为您不应该信任存储在数据库中的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM