When I do this:
$transients = $wpdb->get_col(
"
SELECT option_name
FROM $wpdb->options
WHERE option_name
LIKE '_transient_wb_tt_%'
"
);
It works fine, but when I use prepare like so:
$transients = $wpdb->get_col( $wpdb->prepare(
"
SELECT option_name
FROM %s
WHERE option_name
LIKE '_transient_wb_tt_%'
",
$wpdb->options
) );
It doesn’t work, what am I doing wrong here?
I agree with @bainternet. You don’t need
$wpdb->prepare
. There isn’t any user supplied content.The answer to the question is that to get a wildcard
%
to pass throughprepare
you need to double it in your code.Try that or this if you want a good look at the generated query:
Other than being unnecessary, using
$wpdb->prepare
like this won’t work. The attempt to useprepare
to swap in the tablename will result in a tablename with quotes around it. That is invalid SQL. The query should be simple:From Codex
The essential part is -> “$like = $wild . $wpdb->esc_like( $find ) . $wild;”