Evaluate PHP code from MySQL database
by Geethalakshmi[ Edit ] 2010-10-14 14:45:00
Evaluate PHP code from MySQL database
Here is how you do it.
Open up the page in which you print the content from the database table. For example,
<?php
echo $content;
?>
If you simply echo the code, then the PHP code will not be evaluated. Lets say that your text in your MySQL field looks like this:
Text in MySQL field:
<h1>Page Name</h1>
This is some normal text that I want to put on the page. But I also want to
<?php
echo "print out this text.";
?>
If you just echoed the text like before, the PHP will mess up. But if you eval'ed the code, like this:
<?php
eval($content);
?>
Then you would probably get an error from PHP about the < tag.
This is because since the text being evaluted was started from PHP in the page, then it thinks it is still reading PHP. Therefore you have to end the PHP before it starts evaluating the HTML. You can either add this to every file in the database, or add it on the eval line (probably the best solution). But, if you do this, I assume that you have a mixed HTML and PHP field, and the PHP code has PHP start and end tags, like the sample entry I have above. If it is just PHP, then you can use the code just above this paragraph and it will work. But if you have HTML and PHP, use the code below.
Final Solution
Use this code to evaluate a mixed HTML and PHP field from MySQL, where PHP has start and end tags within the code.
<?php
eval('?>' . $content . '<?php ');
?>