Reputation: 1638
I have this requirement that states that I must have a function signature as a value in a database column. So what I need to do is get that value that will then call the function in code. For example....let's say I have a column in a database named "Signature" that has a value of SendMail();. I need to be able to get the return value and then store it in another table. The problem is when I use the eval function on $i['Signature'] the function gets called, but I can't get the return value. What am I doing wrong? Here's an example:
$query = "SELECT signature FROM tbl WHERE id = 1 ";
$signature = mysql_query($query);
while ($i = mysql_fetch_array($signature)){
//Here the function is being called properly but I can't get the return value.
$email= eval($i['Signature']);
if(mysql_num_rows($i) == 0){
//insert the returned value into another table.
$insertString = "INSERT INTO tbl (email) VALUES ('$email')";
}
}
function SendMail()
{
$email= 'Have a great day'
return $email;
}
Also I'm bound to get some "eval is evil" comments, so if there's a better way please advise.
Upvotes: 0
Views: 59
Reputation: 36955
You would be better off storing the name of the function as a string in one column, and the arguments to pass to the function as a serialised array in another column. Then you could use call_user_func
to get a return value from the function without using eval()
Upvotes: 1
Reputation: 28864
from the docs
eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned.
Therefore you should add return
:
$email= eval('return ' . $i['Signature']);
alternatively:
eval('$email = ' . $i['Signature']);
Upvotes: 3