Jump to content

[SOLVED] embed php in mysql table field?


paulmo

Recommended Posts

possible to put php variable (ex. $name), in text field, so that when row is matched and echoed, php executes. an example, matching "car":

"Cars get us from place to place. The Camry is a good car, Jim. You should drive it." //Camry and Jim being the variables that would be embedded anywhere in field text.

 

thanks for help.

Link to comment
Share on other sites

is my question or language in some way inappropriate, thorpe?

 

No, I'm simply stating that if you think storing php in your database is the solution to your problem, your likely not doing things the right way.

 

You can use eval to parse a string of php code. Really though, I think your likely to find a better / safer and mnore efficient solution if you actually tell us why you think storing your variables in your database is needed.

Link to comment
Share on other sites

ok, thanks, i'll try eval(). i just want to greet user anywhere within field text that's being echoed. plus, since i'll be joining tables, if i could put all information in tables instead of some in php code (greetings) and the rest in tables, might be more streamlined. i really don't care about that though...the main purpose would be greeting within the table text, to give the data a personalized touch.

 

if safety is an issue like you seem to suggest then i would keep php and mysql data separate. 

Link to comment
Share on other sites

You should be using a template system. In the simplest form, this involves using place-holders in the content that is simply replaced with the correct values at runtime.

 

$template = "Cars get us from place to place. The {temp_car} is a good car, {temp_name}. You should drive it.";

$place_holder_array = array();
$place_holder_array[0] = "{temp_car}";
$place_holder_array[1] = "{temp_name}";

$replace_array = array();
$replace_array[0] = "Camry";
$replace_array[1] = "Jim";

$content = str_ireplace($place_holder_array, $replace_array, $template);

 

 

 

 

Link to comment
Share on other sites

thanks for the array template code. are the replace values camry and jim from a submitted form?

 

what i have at the moment is text search box as exploded array, that searches and echoes text rows. (there are some if statements before going to database.) perhaps i'll use a hybrid approach with your template idea (and/or parse eval() which i still have to check out).

 

 

Link to comment
Share on other sites

The example in the manual shows how to use eval() in your code to execute a string (gotten from your database) containing php code. The eval() is part of your application code, not part of the string containing the php code you want to execute.

 

eval() is very slow. It calls the php parser/tokenizer/runtime engine to evaluate and execute the php code in the string it is passed. For a simple variable replace operation, the code I posted above is 2-3 orders of magnitude faster than using eval().

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.