Storing PHP code in MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Storing PHP code in MySQL

Post by rhecker »

I am trying to store all the unique page content for a website in a table. The problem is that on some pages the content is PHP. MySQL won't seem to let me enter content entered from a form textarea when it contains PHP start or end tags (<?php). The database simply won't update the record.

I need to know what needs to be done to make it store records that contain these tags.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Storing PHP code in MySQL

Post by liljester »

are you sure storing EVERYTHING in mysql is your best option? in 10 years of php coding ive never encountered a project that required me to save php code in mysql...

and to answer your question, you would probaby have to use addslashes or some other sort of safety function to render your php code harmless to mysql.

i still think its a bad idea though!
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Storing PHP code in MySQL

Post by JakeJ »

I would have to agree. Generally not a good idea to store that.

Perhaps you could explain what your objective is and we'll all see what we can do to provide an acceptable method.

Unless you are trying to make a searchable index of web code, I can't see any reason to store php code.

Perhaps you could store the php pages as binary object for later retrieval.

But that depends on the objective, let us know. Thanks.
Bind
Forum Contributor
Posts: 102
Joined: Wed Feb 03, 2010 1:22 am

Re: Storing PHP code in MySQL

Post by Bind »

rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: Storing PHP code in MySQL

Post by rhecker »

Thanks for the responses.

The website is multi-lingual so page content is determined by the visitor's language choice. This works fine when the content is just html, text and images. But a few page contents are built dynamically based on records in other tables in the database, such as a list of instructor biographies, with their photos.

My goal is to have all pages in the website follow a pattern. This is important because of the added complexity of managing three languages on the site. The dynamicly generated drop-down menu calls each "page" based on its page_id, the language choice, and the template attached to the page record. I have created a template that is identical to the basic template except that it uses eval($content) instead of echo($content). This works fine for two pages that contain PHP, but not for one.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Storing PHP code in MySQL

Post by liljester »

even given your requirements, you could easily load different language content without storing php code in the database.
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: Storing PHP code in MySQL

Post by rhecker »

Yes, I can think of two methods of doing this without storing the code in the database. One way would be to use include($content), where content would be the name of an include file that contains the code. Another would be to simply make a "template" that wouldn't really be a template, but would be a page containing all the code.

But the problem with those two methods is that they become exceptions to the strict pattern I believe it is necessary to follow. I want this website to have only three template variations--basic, code, and forms. I don't want to clutter up the file structure with numerous "templates" that only work for a single unique purpose. Eventually this website will be very large, and we've all seen how easy it can be for a website to become difficult to manage because of all the structural variations and exceptions to rules.

Therefore, even though I CAN avoid puting the code in the database, I really WANT to put it there.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Storing PHP code in MySQL

Post by liljester »

well good luck with that. also keep in mind the extra strain on your database server having to serve up that much more content, you may need to adjust the hardware accordingly.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Storing PHP code in MySQL

Post by JakeJ »

Seriously, if your site gets BIG, your method will actually cripple your site to the point of making it unusable.

For a language templating system, you don't need to store anything else but the actual text being displayed on the page. Keep all the text in one file and just have a switching system at the beginning to determine which language gets displayed. Each of your text strings will of course need to be assigned to a variable.

Then, on your php pages, you just need a single include and that will load all the options. Although, it wouldn't be difficult to add a 4th category to your templating sytems that is simply called language. That method would mean a folder called language and each page in that folder would mirror the name of the html(or php) file that it will be used it. Then you would just need a single include on each page.

Any large site it tough to manage. You could also use a content management system, there are several of them out there and they can do the language handling for you.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Storing PHP code in MySQL

Post by greyhoundcode »

Don't mean to dissemble, but don't ModX, TXP and other CMSs do just that? Allow editing and storage of PHP in the database for the purpose of plugins?
Post Reply