Wednesday, June 15, 2022
HomeWordPress Development$wpdb->get_results(...) with $wpdb->put together(...) returns empty array regardless of appropriate question

$wpdb->get_results(…) with $wpdb->put together(…) returns empty array regardless of appropriate question


This string, meta_value LIKE '%user-input-value%'", incorporates share wilcards (%) that point out that we’re on the lookout for a row the place meta_value column incorporates user-input-value as a part of its worth.

$wpdb->put together converts them into hashes, and the MySQL server will get one thing like this: meta_value LIKE '{399038939300d2c307c53b29a166ee90101da7b2aba7978a898694010bf1bfe6}user-input-value{399038939300d2c307c53b29a166ee90101da7b2aba7978a898694010bf1bfe6}'. Naturally, I used to be getting no outcomes!

The wpdb::put together web page reads, ‘Literal share indicators (%) within the question string have to be written as %%. Share wildcards (for instance, to make use of in LIKE syntax) have to be handed through a substitution argument containing the whole LIKE string, these can’t be inserted immediately within the question string. Additionally see wpdb::esc_like().’

So, the LIKE half have to be ready like this (instance from the wpdb::esc_like() web page):

$wild = '%';
$discover = 'solely 43% of planets';
$like = $wild . $wpdb->esc_like( $discover ) . $wild;
$sql  = $wpdb->put together( "SELECT * FROM $wpdb->posts WHERE post_content LIKE %s", $like );

The place $wpdb->esc_like() ought to go earlier than $wpdb->put together().

And if one desires to switch share wildcard placeholders manually, they need to use $wpdb->remove_placeholder_escape() after $wpdb->put together().

I ended up with this code, which labored:

$key = 'user-input-key';
$worth="user-input-value";
$worth="%" . $wpdb->esc_like($worth) . '%';
$question = "SELECT * FROM {$wpdb->prefix}postmeta WHERE meta_key=%s AND meta_value LIKE %s";
$prepared_query = $wpdb->put together($question, array($key,$worth));
$prepared_query = $wpdb->remove_placeholder_escape($prepared_query);
$question = $wpdb->get_results($prepared_query);

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments