Page 1 of 1
Executing PHP code from a database
Posted: Thu Oct 09, 2003 12:04 am
by Drew^2
I have a mySQL database which is storing site content, currently in the form of HTML. I'd like to be able to include PHP code into this, to the extent of functions, database queries etc.
My question being, how would I do this?
Example:
Code: Select all
<p>
This is a test document.
<?
for ($i=0; $i<10; $i++) {
echo function_one($i);
?>
</p>
The above is stored as a text object in the database. I can retrieve it and store it in, say, $myContent.
echo $myContent doesn't actually evaluate the code, eval("\$myContent=\"$myContent\";"); doesn't do the functions.
include() is what I need, but this is a website, and writing to a file, even a temporary file, seems like a waste of time and just asking for conflicts.
Any suggestions would be very much appreciated.
Posted: Fri Oct 10, 2003 4:55 am
by twigletmac
Personally, I would not store functions and database queries in the database. Instead I would (and do) store these as ordinary PHP files.
Mac
Posted: Fri Oct 10, 2003 4:14 pm
by Cruzado_Mainfrm
how can u store a php file if when u get it back from the DB is just plain text????
Posted: Fri Oct 10, 2003 11:26 pm
by fractalvibes
Don't know why you would want to do this. You would be better off creating groups of functions to simply include or require, methinks.
What you would pull out of the database would just be a string. I don't think it would get executed.
fv
Posted: Sat Oct 11, 2003 9:09 pm
by Drew^2
I want to do this so I can include function calls and executed PHP content in this site content. For the most part, the site content will be standard HTML, for which this will work fine, but some pages will require PHP-generated content.
I've been debating having groups of functions and what-not, but I'm at a loss as to how to get them to be called at the appropriate part of the content.
Posted: Sat Oct 11, 2003 9:31 pm
by McGruff
Are you sure you want to store html in a database? Normally this would be defined in html files and then the files are included in a script. That way you can edit the html easily - how do you edit html stored in your db?
Dynamic content in the html files can be added simply by <?php echo $stuff; ?>. Declare $stuff prior to including the html file.
Template engines work on a similar basis, but block php scripts in templates. Instead, custom placeholders are used to denote dynamic content, and some regex'ing replaces these with a php var or function output.
Posted: Mon Oct 13, 2003 12:49 am
by Drew^2
Yes, very sure that it needs to be stored in the database. It's a kind of online dynamic editing system, database is the best solution.
Thanks for the advice, I think I see what I need to do - have sets of includable files which return the appropriate HTML and have placeholders in the document content.
That will work, I think. Much better than the other way I was thinking of.
Thank you!