echo php from db

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
Benneth
Forum Newbie
Posts: 1
Joined: Sun Oct 10, 2004 4:43 am

echo php from db

Post by Benneth »

Hi Guys,

I have searched the forums but have not found anything related to what i want to do.

Basically the subject says it all. I want to echo ( or 'use' ) php from a database.

I currently have it working fine for html, but when i chuck in some php it seems to fall over.

Ok an example...

Code: Select all

while ( $row = mysql_fetch_array($result) ) {
       	echo $row['strContent'];
}
So if i chuck some html in strContent in the db it would echo it fine, in html.

example

Code: Select all

<h1>This is heading 1</h1> // so here, it would decode the html and display "This is heading 1"
Now, if i chuck in some PHP in strContent nothing appears to happen.

example, if strContent contained

Code: Select all

<? echo "Hi - this just will not display!"; ?>
I used code tags because it's stored as plain text so not really php... but i want it to be displayed as php on my page.

Now i may have missed something really simple here, perhaps a php setting for echo to properly decode and display php with echo ? is there another command to use? I tried print and return... no luck here.

Any help would be much appreciated! Thanks guys
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

check out the [php_man]eval[/php_man]() function, it lets you execute a string as php code
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

But the best thing is to avoid having to use eval() by not storing PHP in a database.

Mac
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

and it's very unsafe. instead of this idea you can include()...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd suggest properly usage of templates, with the content itself being stored in the database or in local files would be the "best practice" solution to your set up.

If you must have code in the database, I seriously hope you have spectacular security.
Post Reply