php code in 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
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

php code in database

Post by ol4pr0 »

Oke maby kinda stupid but there must be a way to be able to do this.

i tried out a simple thing like the following

I had this code in a database field

Code: Select all

<?
echo 'Hello World';
?>
Than i retrieved that row from the database trying to have it displayed on the page. However it didnt do that.

When just using text like "Hello World", it would nicely print out "Hello World"

Is there a way to be able to retrieve html and php code from the database ? Or what is the best way to do this.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]eval[/php_man].. be careful though ;)
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Howcome what is the downside of using that ? Is there a better way ?


edit: Every html code has to be echo'd with php .. Hmm can that be changed ?
Last edited by ol4pr0 on Fri Aug 27, 2004 2:42 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

blindly eval'ing code can create problems if someone is able to insert code that say... deletes the entire site. :)
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

What is a other option.. besides putting the whole code in pages lol, i want to be possibel to get as much as i can from the database. However some pages include some queries. So how to get them into the code that i am retrieving from the database

Lets say

#in database
<?
<body>
</body>
?>
Now i want to insert content. which is also stored in database.
Which needs to be between the body tags. or tables.. or whatever..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if you want it for content... look into using templates. for instance, assume the following is stored in a field inside the database

Code: Select all

&lt;body&gt;&#123;DATA&#125;&lt;/body&gt;

Code: Select all

echo str_replace('{DATA}','this is inserted content',$text);
Post Reply