0

I have written a function that is supposed to be able to generate a number of input fields which will be commonly used in a private web application.

Details of these fields are stored in the mysql database. There is a varchar field which holds the name of the function that generates the related text for a dropdown, as well as the associative key to retrieve the right string from the function's result.

The variable function name does not call the function. It worked in an earlier simplified iteration using only mysqli_fetch_row rather than mysqli_fetch_assoc.

I'd like to get it working with associative keys. (I can see no reason why associative keys could be the problem).

I've tried various forms of eval and exec. I can get a lite version to work with mysqli_fetch_row.

I have followed instructions here: Store function name in database and then execute it

I haven't been able to find anything else that directly adresses the problem.

$funcResult = $func($fldNmValue)[$funcKey];

doesnt't work

$funcResult = rank($fldNmValue)[$funcKey];

works

'rank' is one of the function names in the database

it is pulled from the database by:

$query = "SELECT fieldFunction,fieldFunctionKey FROM fieldType WHERE fieldTypeId='$fldTypeId'";
$result = db_query($query);
$data = db_fetch_assoc($result);
$func = $data['fieldFunction'];

I have verified that $func contains valid function names like 'rank' or 'castDetails'.

printing "$func($fldNmValue)" gives, for example:

castDetails(2)

as expected.

PHP refuses to execute $funcResult = $func($fldNmValue)[$funcKey]; and simply exits.

I had expected it to function like it did when using mysqli_fetch_rows.

Derek Pollard
  • 6,953
  • 6
  • 39
  • 59
SolonLost
  • 9
  • 2
  • 4
    This is a bad idea; your data should be independent of your code – Derek Pollard Apr 19 '19 at 14:31
  • Not sure how this relates to the question, but it's quite seperate, thanks. – SolonLost Apr 19 '19 at 14:52
  • I can't tell from your code but simply it does work https://3v4l.org/6Hm3j – AbraCadaver Apr 19 '19 at 14:55
  • 1
    Can you show the code that you have working with `mysqli_fetch_row()`? When assigning a value directly to `$func`, your code seems to work just fine. – Patrick Q Apr 19 '19 at 14:55
  • 1
    Also, do you have `error_reporting(E_ALL); ini_set('display_errors', '1');` – AbraCadaver Apr 19 '19 at 14:57
  • 1
    Can you clarify what you mean by "PHP refuses to execute ..."? PHP does not "simply exit" other than in the case of fatal errors, which should be accompanied by an error message. – Patrick Q Apr 19 '19 at 14:58
  • take a look at call_user_func here https://www.php.net/manual/fr/function.call-user-func.php – A.Marwan Apr 19 '19 at 15:04
  • Right, you guys certainly got me on the right track. Turned out that the function CastDetails ran some code that wonked out when there was a certain type of result from the database. Trying to put it back together with mysqli_fetch_row led to the discovery after that is was just following the error reporting. Thanks for the help! – SolonLost Apr 19 '19 at 18:04

0 Answers0