Storing PHP code in MySQL
Moderator: General Moderators
Storing PHP code in MySQL
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.
I need to know what needs to be done to make it store records that contain these tags.
Re: Storing PHP code in MySQL
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!
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!
Re: Storing PHP code in MySQL
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.
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.
Re: Storing PHP code in MySQL
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.
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.
Re: Storing PHP code in MySQL
even given your requirements, you could easily load different language content without storing php code in the database.
Re: Storing PHP code in MySQL
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.
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.
Re: Storing PHP code in MySQL
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.
Re: Storing PHP code in MySQL
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.
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.
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: Storing PHP code in MySQL
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?