0

I have a function name stored in my db but I get a syntax error when I try calling it.

$downloads = $wpdb->get_results( "SELECT function_name FROM master_download" );

foreach( $downloads as $download ) :
  $function_name = $download->function_name;
  eval( $function_name );
endforeach;

Value in db is:

csv_upload_measure_custom( $cell, $measure, $measure_start, $measure_end, $loc_id )

Error code:

Parse error: syntax error, unexpected end of file in /nas/content/staging/justreport/wp-content/themes/yardstick/template-data.php(373) : eval()'d code on line 1

Please can someone help me with what I am doing wrong?

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
Dudley
  • 63
  • 12
  • 1
    _" but I get a syntax error"_ - Please share it. I'm also curious about _why_ you have a function in your database like that? – M. Eriksson Jan 27 '18 at 12:50
  • Always include the full and exact error message. – erik258 Jan 27 '18 at 12:50
  • 2
    You need to add a `;` at the end of the function. Do a `eval( $function_name . ';' );` – M. Eriksson Jan 27 '18 at 12:59
  • @MagnusEriksson thanks. My site has 43 different csv file uploads depending on settings so I have put each csv layout in a function and then call the function name from the db. I'm newbie to programming so don't do things the most logical way – Dudley Jan 27 '18 at 13:11
  • Since you haven't told us enough for us to really understand what you're doing, I can't give you a better option. But in general, putting PHP code in the DB and using `eval()` to run parts of your code are two big anti-patterns. It can usually be done in much more elegant ways. If you need to update the call or add an argument or what ever, you need to update your DB + you can't really read the code to understand what it's doing. – M. Eriksson Jan 27 '18 at 13:17

0 Answers0