How do I escape a table name or column name in SQL? esc_sql doesn’t do this


If you want to escape string values in an SQL query, you can use WordPress’s esc_sql function:


$wpdb->get_var( "SELECT * FROM something WHERE foo = '" . esc_sql( $foo ) . "'" );

You can also use the much more convenient prepare function like this:


        "SELECT * FROM something WHERE foo = %s",

However, esc_sql is not suitable for escaping table names or column names, (only string values). And there is no way to use prepare for escaping table names or column names.

How can I escape $foo and $bar properly in this example SQL query?

SELECT * FROM $foo WHERE $bar = "example";
Flimm 3 months 0 Answers 9 views 0

Leave an answer