Executing PHP code from a database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Drew^2
Forum Newbie
Posts: 3
Joined: Thu Oct 09, 2003 12:04 am

Executing PHP code from a database

Post 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++) &#123;
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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

how can u store a php file if when u get it back from the DB is just plain text????
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post 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
Drew^2
Forum Newbie
Posts: 3
Joined: Thu Oct 09, 2003 12:04 am

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Drew^2
Forum Newbie
Posts: 3
Joined: Thu Oct 09, 2003 12:04 am

Post 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!
Post Reply