Really simple query giving error in SQL syntax

Don’t use $wpdb->prepare with table names. prepare will quote your table names that will result in incorrect SQL.

It is unnecessary overhead as well. Your table name should never, and in your case isn’t, be user-supplied data. You have no need to escape it. ‘prepare’ is for use on user-supplied data, such as data from a form or a $_GET parameter.

You have no user-supplied data so all you need is

$wpdb->get_results("SELECT * FROM {$tablename}");

Leave a Comment