Jump to content

PHP function in MySQL DB field and execute it? Is this possible?


Recommended Posts

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?

 

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().

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 :)

 

 

 

 

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.

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().

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.