The prepare() method escapes %s. The second piece of code you listed breaks because quotation marks are added to the table name, hence it doesn’t match what’s in the DB.
The first piece of code works because it’s a straight string replacement hence matching the name of the table in the database.
What is the error message you are getting?
HTH
Related Posts:
- Differences between wpdb->get_results() and wpdb->query()
- How to Use Wildcards in $wpdb Queries Using $wpdb->get_results & $wpdb->prepare?
- wpdb get posts by taxonomy SQL
- Multipart/formatted MySQL query problem
- Custom $wpdb Query for Custom Post Type by Category
- $wpdb->get_var not returning a result
- Most effective use of DB querys
- $wpdb->query() vs. $wpdb->get_results() vs. phpMyAdmin
- wpdb::prepare() isn’t working
- $wpdb doesn’t appear to work on page inside of a plugin
- Strange string in console from wpdb query
- Using $wpdb->update but confused on the WHERE in and SET
- What’s wrong with my $wpdb prepare?
- Make one query for adding entries to database
- How to execute a SQL-query which contains multiple queries using $wpdb->query?
- Alter a specific query on WordPress
- wpdb->term_taxonomy count posts from both published and private posts?
- wpdb print all post meta
- Plugin will sort users by usermeta
- wpdb query problem to access previous 3 days posts
- How to write: $wpdb->update having WHERE NOT value pair in the array
- I can’t figure out what’s wrong with this statement. $wpdb->query update
- Multiple Category Query
- Use mysql_query instead of $wpdb query
- wpdb COALESCE won’t work
- Why Query is returning empty array?
- Custom Query – Based on user input
- Custom SQL Query passing array to WHERE p.id
- Get posts from category with custom query
- Custom Query returns array, but I can’t see it’s elements
- Valid SQL query return empty
- mysql query – how to escape apostrophe?
- $wpdb->prepare affecting the query?
- prepare function sql safe method
- Custom select query two tables by a meta key
- Updating with $qpdb->query() always returns 0 rows affected
- How to return count of custom post type posts with a specific custom field value via $wpdb?
- WordPress Mysql query and Duplicate
- How do I query for posts by custom meta and those that have been stickied?
- Help with Related Posts Function
- wpdb update multiple row?
- Formatting a date/time returned from a custom $wpdb query
- Improve performance of slow query
- How do I get specific readable results from this query and array results
- implementing my wp_query using $wpdb class
- Exclude Posts from a Widget
- Fastest and most efficient SQL query to check if UID exists
- Query String for the WP_QUERY parameters
- Use $wpdb->get_results with filter based on array
- $wpdb query for price in custom field value
- Display result from custom post meta query
- Wpdb empty result only if language is Russian
- Run an update query in a function
- Include custom table in query
- How to fix this insert? Don’t work for me!
- Problem on wpdb queries, wp_insert_post, wp_insert_user etc. The query runs twice
- Update vs Insert logic but the last key is always inserted?
- There is way to improve this query? wordpress get post data and some meta data
- $wpdb: how to use a query as a base for another query?
- Wpdb generates too many queries
- get_var result empty for query of custom table in plugin admin
- Custom Query for searching through custom fields
- Mysql query and odd results
- How to reference same column name but different table in custom db query
- Retrieving custom field array value through db query
- How to get one result using wpdb class?
- Delete query won’t run
- query posts only works on the first page
- update $wpdb one query
- Is their any way to Extend WPDB class and Overwrite the Default Query Function
- Different Main Navigation per category
- Show All Posts Insert Edit Link
- Get posts by category with pure SQL query
- can’t get query to order posts by acf datepicker
- Help with wordpress custom query and advanced custom fields plugin
- Mysql / WordPress killing my server with 80k users [closed]
- Query all posts and not repeat the same tag
- Deleting data from a custom table in WordPress
- Any possible way to make $wpdb->get_results() return anything else than array?
- Missing posts in a query
- Protect sequence of pages with same password for each of two groups of users
- How is it possible to get top comment from all children?
- Iterating through $wpdb query without using get_results for large query results
- All in One Calendar Plugin Custom Post Type Query [closed]
- Get Posts from Last 24 hours and Sort them via GD Star Rating
- Taxonomy filter under Polylang
- How to create an overview of posts with the same tag?
- create a link to a random post within the current category
- JetEngine Query Builder – order by value from repeater
- Change pure SQL database query to WordPress post query?
- Heavy meta query causing SQL crash
- Sort by postmeta on when searching
- Pulling an ACF into a query
- Speed up search query that searches in post meta?
- Using cron for multiple queries
- very slow wordpress query with default query
- Custom Query problem access the values
- Rewrite Query_Var URL Parameter with Slug
- Can’t See Media Queries with Inspect Tool [closed]
- How to show sticky posts on all pages of the pagination, not just the first page?