paulmo Posted February 5, 2009 Share Posted February 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/ Share on other sites More sharing options...
aschk Posted February 5, 2009 Share Posted February 5, 2009 Yes it's possible but I would advise against it. Databases are for storing data, not variables... Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/#findComment-755182 Share on other sites More sharing options...
paulmo Posted February 5, 2009 Author Share Posted February 5, 2009 ok thanks, what would the syntax be, please? it's not {$post_name}. Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/#findComment-755185 Share on other sites More sharing options...
trq Posted February 5, 2009 Share Posted February 5, 2009 ok thanks, what would the syntax be? can't seem to find anything on this. If this is your solution to your problem your likely going about things the wrong way. Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/#findComment-755189 Share on other sites More sharing options...
paulmo Posted February 5, 2009 Author Share Posted February 5, 2009 sorry, revised above. if i had the solution, probably wouldn't raise the question here. is my question or language in some way inappropriate, thorpe? Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/#findComment-755193 Share on other sites More sharing options...
trq Posted February 5, 2009 Share Posted February 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/#findComment-755194 Share on other sites More sharing options...
paulmo Posted February 5, 2009 Author Share Posted February 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/#findComment-755260 Share on other sites More sharing options...
PFMaBiSmAd Posted February 5, 2009 Share Posted February 5, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/#findComment-755273 Share on other sites More sharing options...
paulmo Posted February 6, 2009 Author Share Posted February 6, 2009 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). Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/#findComment-755654 Share on other sites More sharing options...
fenway Posted February 6, 2009 Share Posted February 6, 2009 This is getting very far away from mysql... Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/#findComment-755812 Share on other sites More sharing options...
paulmo Posted February 8, 2009 Author Share Posted February 8, 2009 i've tried parse {eval ($name)} in the mysql field text and it's echoing {eval ($name)}. the manual mentions the function is useful in database text but doesn't give an example how to do this. suggestion please? thanks Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/#findComment-757436 Share on other sites More sharing options...
PFMaBiSmAd Posted February 8, 2009 Share Posted February 8, 2009 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(). Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/#findComment-757485 Share on other sites More sharing options...
paulmo Posted February 9, 2009 Author Share Posted February 9, 2009 PFMaBiSmAd thanks for the code and believe me i'll try to use it if it interacts somehow with my database data, which i'm not seeing in the array example. could you explain how/if it will do this, please? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/#findComment-757813 Share on other sites More sharing options...
trq Posted February 9, 2009 Share Posted February 9, 2009 In PFMaBiSmAd's example, $template represents the data stored in your database. Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/#findComment-757970 Share on other sites More sharing options...
paulmo Posted February 9, 2009 Author Share Posted February 9, 2009 great, thanks! i see the concept now. Quote Link to comment https://forums.phpfreaks.com/topic/143917-solved-embed-php-in-mysql-table-field/#findComment-758105 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.