Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
executing php code stored in sql table
Old 12-30-2009, 09:02 PM executing php code stored in sql table
Average Talker

Posts: 27
Trades: 0
Ok so I recently kind of got the hang of SQL tables and sorry for my excitement, but ITS FREAKIN AWESOME
I only have one problem remaining left, how can I insert php code into the table, so that when I access it by using php, it will actually execute?
I mean its like this
My whole page layout values are stored inside the sql table, and I am having no difficulties to retireve them by using php, but if I store a php code into the sql table and try to retrieve it by using echo command on php all it will do is simply print out the actual php code rather than executing it. Is there any way to fix that? Thanks in advance for all your help
__________________
Those who can: learn. Those who can't: teach.
Cinatas is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-30-2009, 09:29 PM Re: executing php code stored in sql table
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Before I show you how, you should know that storing PHP code in your database might not be the best idea. For one thing, evaluating the code will require more resources than normally executing code. Also, if you plan on allowing users submit code which will then be evaluated, you're setting yourself up for disaster.

PHP provides a function for evaluating code in a string: eval(). Basically you use it like this:

PHP Code:
$code//the code you pulled from the database
eval($code); 
There are a couple of catches, however. For one, any output resulting from the eval'd code will not be sent. One workaround for this problem is to append an output buffer into the eval'd code:

PHP Code:
$code;

$pre 'ob_start(); ';
$post ' $output = ob_get_clean();';

eval(
$pre $code $post);
echo 
$output
Notice that variables created by eval persist for the remainder of the script. eval can also modify existing variables so you may want to be careful to avoid collisions (eval making use of or overwriting a pre-existing variable in a way that was not intended).

Also...
eval assumes you are already in PHP mode (in other words you should not begin your eval'd code with an opening tag <?php or end with a closing tag ?>).

Just to reiterate: using eval to run code stored in a database isn't a very common task, you may want to rethink your approach.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 12-30-2009 at 10:21 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 01-02-2010, 03:55 PM Re: executing php code stored in sql table
choskins102's Avatar
Super Talker

Posts: 137
Name: Casey
Trades: 3
Quote:
Originally Posted by NullPointer View Post
Before I show you how, you should know that storing PHP code in your database might not be the best idea. For one thing, evaluating the code will require more resources than normally executing code. Also, if you plan on allowing users submit code which will then be evaluated, you're setting yourself up for disaster.
I agree :-)
choskins102 is offline
Reply With Quote
View Public Profile
 
Old 01-03-2010, 12:59 PM Re: executing php code stored in sql table
freezea's Avatar
Experienced Talker

Posts: 45
Trades: 0
Hi.
Please refer to the url below:
http://www.devarticles.com/c/a/PHP/E...ures-With-PHP/
I wish it will be helpful.
Regards,
freezea.
__________________

Please login or register to view this content. Registration is FREE
: Web-based Excel-like Java reporting tool.
freezea is offline
Reply With Quote
View Public Profile
 
Old 01-03-2010, 03:03 PM Re: executing php code stored in sql table
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by freezea View Post
Hi.
Please refer to the url below:
http://www.devarticles.com/c/a/PHP/E...ures-With-PHP/
I wish it will be helpful.
Regards,
freezea.
Stored procedures are something else entirely.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to executing php code stored in sql table
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15359 seconds with 12 queries