我有這個要求,指出我必須有一個函數簽名作爲數據庫列中的值。所以我需要做的是獲取那個值,然後調用代碼中的函數。例如....假設我在一個名爲「Signature」的數據庫中有一個列,其值爲SendMail();.我需要能夠獲得返回值,然後將其存儲在另一個表中。問題是當我在$ i ['Signature']函數被調用時使用eval函數時,但我無法獲得返回值。我究竟做錯了什麼?這裏有一個例子:從PHP中的數據庫調用函數
$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;
}
當然我也會勢必會得到一些「EVAL被邪惡」的意見,因此,如果有一個更好的辦法,請告知。
如果必須堅持使用eval,至少應檢查簽名列的內容是否實際上首先是有效的。也許通過'function_exists'函數:http://php.net/manual/en/function.function-exists.php – imm