slyte33 Posted October 29, 2010 Share Posted October 29, 2010 Let's say I have a mysql table named 'function' and a field named 'body' with 1 row. The 'body' field contains 'Echo out the number 1000 with a comma. ".number_format(1000).". Simple Example.' As you can see there is a function; number_format(); in the database, but is it possible to execute it onto a live webpage and be displayed correctly? Quote Link to comment https://forums.phpfreaks.com/topic/217221-php-function-in-mysql-db-field-and-execute-it-is-this-possible/ Share on other sites More sharing options...
Psycho Posted October 29, 2010 Share Posted October 29, 2010 Yes it is possible, but any programmer decent programmer would tell you that is a very poor method of doing what you are trying to achieve. You should never need to execute a string as PHP code. Why not evaluate "number_format(1000)" before you store the data then just store the complete string? Then you simply need to grab the value from the database and echo it to the page. There is a function for this, but if you think you need to use it you need to do some research to verify that there isn't a more appropriate way of accomplishing the same task. Once you are convinced that you still need to execute a string as code, you need to go back and research some more because you obviously missed something. Although, what you are asking for is NOT the solution you should be following, the answer to your question is to use eval(). Quote Link to comment https://forums.phpfreaks.com/topic/217221-php-function-in-mysql-db-field-and-execute-it-is-this-possible/#findComment-1128077 Share on other sites More sharing options...
slyte33 Posted October 29, 2010 Author Share Posted October 29, 2010 Thanks mjdamato. The reason I need to use this is because I run an online game, and have a set of 11 skills a player can do. I have stored the descriptions of each skill on several things. (captions, game guide, tipboxes) In the game guide, all information is kept in the database and executed. So I really need to execute it with a function; box(1,$db); function box($id,$db) { $box=$db->execute("select * from tipbox where id=$id"); $b=$box->fetchrow(); return stripslashes($b['desc']); } So in the "gameguide" table, under the field "body" I need to have it display: Blacksmithing - ".box(1,$db)." and so on. Thank you for the answer Quote Link to comment https://forums.phpfreaks.com/topic/217221-php-function-in-mysql-db-field-and-execute-it-is-this-possible/#findComment-1128080 Share on other sites More sharing options...
Psycho Posted October 29, 2010 Share Posted October 29, 2010 So you are saying that out of all the hundreds of thousands of applications that don't need to use eval() your app is a special case? Not buying it. There are many reasons that you should never use eval() - and I'm not going to go into them here. There are plenty of resources available to explain why it is a very bad decision. Quote Link to comment https://forums.phpfreaks.com/topic/217221-php-function-in-mysql-db-field-and-execute-it-is-this-possible/#findComment-1128116 Share on other sites More sharing options...
slyte33 Posted October 29, 2010 Author Share Posted October 29, 2010 So you are saying that out of all the hundreds of thousands of applications that don't need to use eval() your app is a special case? Not buying it. There are many reasons that you should never use eval() - and I'm not going to go into them here. There are plenty of resources available to explain why it is a very bad decision. You're right. It isn't a special case and I did go and read why eval() was bad. So I simply used a for loop for my function and based it off of that. The code looks a bit more sloppy with a bunch of if/and/or's but it's working well now, without having to use eval(). Quote Link to comment https://forums.phpfreaks.com/topic/217221-php-function-in-mysql-db-field-and-execute-it-is-this-possible/#findComment-1128144 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.