Evaluate PHP code from MySQL database field

Did you ever want to evaluate PHP code that you stored in a MySQL database? With this short tutorial, you can, and you can probably do it with no changes to your database. This is very useful for content management systems that store content in MySQL tables, because you can now put PHP code in with your content and still use it on the page. 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 ');
?>

Hope this helps!

Rate this Tutorial

Rate this Tutorial:

Current Rating:

4.5 out of 54.67/ 5.00 with 52 votes.

Current Comments

35 comments so far (post your own)

Im working on a cms atm, and i get these errors alot, but I do not understand. You have not explained what the function actually does.

Seb

Posted by Seb (http://mus1c.net http://dicdif.org)on Thursday, 04.21.05 @ 03:35pm | #501

Nice tutorial

Posted by Joe on Friday, 04.29.05 @ 10:50pm | #517

Just what I was looking for. Didn't even think of adding the '?>' and '<?php ' to it. Nice job.

Posted by Paul on Thursday, 05.26.05 @ 05:49pm | #551

I may not understand perfectly how it works, but it does.
Thank you so much!!!!
I wish this topic hadn't been so hard to find on the web. Surely its not an obscure issue with Mysql and PHP being so often used?

Posted by Cailean (www.highlandcathedral.org)on Saturday, 06.25.05 @ 03:06am | #582

Doesn't quite work with me-- I think I have PHP 4.3. I keep getting an 'unexpected $' (unexpected end-of-file) error.

Posted by Dustin on Thursday, 08.18.05 @ 10:30pm | #660

Check to make sure that you have a semicolon at the end of your php lines.

Posted by Brian Zimmer on Friday, 08.19.05 @ 03:31pm | #661

Excellent! I was about to tear my hair out over this one.... Thanks for saving me from baldness!

Posted by w.Gilk on Saturday, 11.19.05 @ 06:33pm | #730

Thanks!!! Powerful and simple

Posted by Tom on Monday, 12.19.05 @ 02:45pm | #753

I agree. It's simple yet powerful. This is a great help! Thanks :)

Posted by jhonnette on Monday, 02.27.06 @ 08:39pm | #876

Confused about the function? As usual, look at it's page/;
http://us3.php.net/eval

Posted by Bo on Tuesday, 03.7.06 @ 07:21pm | #891

Dude, you RULE! thanks so much.

Posted by Jake on Monday, 04.3.06 @ 04:55pm | #956

Guys, this is such a big deal when using any of the textarea editors such as FCKeditor, TinyMCE or Xinha. This is a huge lifesaver. THANK YOU!!!

Posted by Wes on Wednesday, 04.26.06 @ 10:45pm | #1014

Awesome! Thanks.

Posted by eWiz on Wednesday, 05.17.06 @ 08:42am | #1052

i love you.

Posted by jeff on Tuesday, 06.6.06 @ 05:56am | #1505

Premo stuff. Thank google for finding this page...

Posted by bigdaddy (www.deltasafe.net)on Monday, 09.18.06 @ 09:00am | #2598

You are a God.

THANK YOU!

Posted by Doug on Saturday, 11.4.06 @ 02:53am | #2647

Very very helpful

Posted by Andrew on Wednesday, 12.20.06 @ 12:15pm | #2683

Hi please can someone explain this to me. It's one of those things that can bug u for days. I dont understand this. Where does the HTML go and where does the PHP code go? could someone give me an example of their scripts that they insert into the database? Would be very greatful, thanks

Posted by Wasim Khamlichi on Monday, 01.8.07 @ 06:26am | #2710

excellent man!!

Posted by Andy Hernandez (http://www.uinteract.com)on Friday, 01.12.07 @ 02:04pm | #2715

great tutorial, thanks a lot

Posted by Tomaž on Thursday, 02.1.07 @ 07:52am | #2748

Thanks for this. Have been trying to figure out how to do this every now and then over the last year.

Posted by Rob on Monday, 02.5.07 @ 04:11pm | #2752

Fantastic. I spent hours trying to figure this out. You solved it for me in 30 seconds! :)

Posted by Mark on Wednesday, 02.14.07 @ 06:46am | #2765

How would this work with a hyperlink with php in it? For example, in a mysql field I have a link : 'http://www.mywebsite.com?id=<?php echo $keyword ?>' Where I want to call up a string inbedded in my php page.

Does this still work? Or do I have to do something a little different?

Posted by Chuck on Friday, 03.2.07 @ 02:26pm | #2779

How would this work with a hyperlink with php in it? For example, in a mysql field I have a link : 'http://www.mywebsite.com?id=<?php echo $keyword ?>' Where I want to call up a string inbedded in my php page.

Does this still work? Or do I have to do something a little different?

Posted by Chuck on Friday, 03.2.07 @ 02:26pm | #2780

How would this work with a hyperlink with php in it? For example, in a mysql field I have a link : 'http://www.mywebsite.com?id=<?php echo $keyword ?>' Where I want to call up a string inbedded in my php page.

Does this still work? Or do I have to do something a little different?

Posted by Chuck on Friday, 03.2.07 @ 02:26pm | #2781

Took me 3 minutes to find the tutorial .. already knew the function .. actually used that ?> tag at a certain point but never fully understood it until i read this .. Thanks a lot .. it's a great tutorial for people who need a good straight answer .. not the usual bs

Posted by Hellbound (www.romtek.net)on Saturday, 03.31.07 @ 05:33pm | #2814

Wow! This is EXACTLY what I needed. I mean to a T. Thx so much for taking the time to post this solution.

Cheers!

Dom

Posted by DomHoo on Friday, 04.13.07 @ 10:47am | #2841

please contact me

Posted by rakeshkannan (www.aiswaryam.com)on Monday, 05.14.07 @ 01:09pm | #2876

I can't even begin to express how thankful I am for this tutorial! I have spent DAYS trying to make a CMS that would allow our client to edit the text for the page via a WYSIWYG editor, but not disturb a form that is in the middle of the page. Instead of the form showing up, I wanted to use special tags "--form--". Those tags would then be replaced with an include function to include the form.php page in that spot. The form.php page has the code to store the data in the database.

This code works great and is exactly what I was looking for!Thank you!!!

Posted by Paul on Wednesday, 08.29.07 @ 12:43pm | #2998

got inspiration from your method:

i got some function that return '$something'. and a html template that contain '{something}'. Now i want to replace '{something}' with '$something' which i store in mysql database, my solution:

$row_mysql['find'] = '{something}'/;
$row_mysql['replace'] = '$something'/;

eval("\$replace = \"".$row_mysql['replace']."\"/;")/;
$body = stripslashes(ereg_replace($row_mysql['find'], $replace, $body))/;

hope this helps...

Posted by fazlee (www.phixelcode.com)on Friday, 08.31.07 @ 11:20am | #3000

Exactly what I was looking for.
Thanks.

Posted by Arif on Wednesday, 11.14.07 @ 09:23am | #3074

thanks for fantastics codes, these really helped me a bunch and if you were a woman, i would have kissed you :)

Posted by joren on Friday, 11.23.07 @ 03:22pm | #3086

Thank you very simple and usefull

Posted by Yashar Rashedi (www.sabadkharid.com)on Thursday, 01.31.08 @ 01:01am | #3180

Thank you very simple and usefull

Posted by Yashar Rashedi (www.sabadkharid.com)on Thursday, 01.31.08 @ 01:16am | #3181

thanks for the info. But by doing this meaning the user can put a php code that can delete system files is it? or is there any ways to prevent this from happening.

Posted by Farid (www.damiaworks.com)on Saturday, 05.10.08 @ 12:03pm | #3329

Leave your comment:

Learn how to add this comment form to your site with our comment form tutorial!

Name:

Email:

URL:

Comments:


 
Guess the letters and numbers
(passphrase riddle)
--
"3",
/J/
&
small 'X' +2 letters,
/H/
&
small 'G' +3 letters
-
(??? - 4) = -2;
:u:
&
→ retype that here
Enter the correct letters and numbers from the image into the text box. This small test serves as access restriction against malicious bots. Simply reload the page if this graphic is too hard to read.
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.