Click to See Complete Forum and Search --> : eval($sql_result)


gert cuykens
10-11-2006, 11:16 PM
i am considering to put my site completly into a sql database and using eval($sql_result) to load my pages. How much performance and resources do i loose compaired with using seperate php files ?

pcthug
10-12-2006, 12:23 AM
Why would one want to index raw php in a database rather than in a filesystem? It makes no sense.

I assume in escaping all variable declarations and double/single quotation mark's etc. as well as executing all code you would use a fair amount of memory. You would also lose functionality and vital information in error output, etc.

Really, I can see no real advantages of such a method.

NogDog
10-12-2006, 01:46 AM
Yeah, unless there's some strong reason to do such a thing, it seems like an unnecessary addition of layers of processing: connect to database, send query, get query results, process results through eval(), and finally execute the PHP code.

I'm not saying that there might not be a good reason, but make sure that there is a reason other than "because I can", and that it outweighs the added complexity and processing demands.

bokeh
10-12-2006, 02:57 AM
Although it is possible to use eval() in this situation it is a misuse. Eval() is for use in situations where the code must be build dynamically based on the content of certain variables. The code in this thread (http://webdeveloper.com/forum/showthread.php?t=122748) is a good example of such a use.

Getting back to the code in the DB issue: I was recently working on a site where there was not a proper file structure and the main content of each page was held in the database. This left me with the same problem: "How do I run code halfway through the string contained in the database?". I didn't want to place executable code in the DB or to use eval(). In the end I put a marker in the DB wherever I wanted to run a piece of code. The marker looked like this: {function_name}. Once I pulled the string from the DB I looked for the markers and if any were found whatever function name was contained between the curly braces was run at that position. This allowed each coded section to be held in the template file as a function while the main content was held in the database without any code being included. The following is the code that was used to evalute the string returned by the DB.

function content_additions($input)
{
if(!function_exists('content_additions_callback'))
{
function content_additions_callback($input_array)
{
$function = $input_array[1];
if(is_callable($function))
{
return call_user_func($function);
}
}
}
return preg_replace_callback('/[{]([^}{]+)[}]/', 'content_additions_callback', $input);
}Maybe not a perfect solution but it does keep the code separate from the DB and does away with the need to eval() anything.

gert cuykens
10-12-2006, 07:11 AM
I think it would be more of a "because i can" kind of thing :D That combined with people calling me about static site changes, that make me feel like i am there secretary. I want to put on my answering machien yoursite.com/do_it_yourself_and_leave_me_alone.php :D