2
$db_item =  $wpdb->get_results($wpdb->prepare( "SELECT * FROM wp_wowhead_items WHERE name LIKE %s", "%". "Hello" . "%") );

This above one works... But the below one doesn't work!

$text = "Hello";
$db_item =  $wpdb->get_results($wpdb->prepare( "SELECT * FROM wp_wowhead_items WHERE name LIKE %s", "%". $text . "%") );

Where am I missing the syntax? I tried almost every combination with quotes, slashes, escapes...

2
  • 2
    out of curiosity could you try "%$text%" Commented Feb 27, 2011 at 8:47
  • this worked... Thanks. The problem is I tried it before, but it was freezing so I thought it wasn't working. Appereantly somewhere else it was giving "%%" and this was freezing the screen and I wasn't able to actually see it was working. Commented Feb 27, 2011 at 9:24

2 Answers 2

3

Wrap it in quotes:

$db_item =  $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM wp_wowhead_items WHERE name LIKE %s", "'%". $text . "%'"));
Sign up to request clarification or add additional context in comments.

1 Comment

@Weptile: Try running your query in mysql client first to make sure it returns any rows or not.
1

Try this:

$db_item =  $wpdb->get_results($wpdb->prepare(
    "SELECT * FROM wp_wowhead_items WHERE name LIKE %s", '%$text%'"
));

6 Comments

thank u, the "'%$text%'" dosen't need " ". just '%$text%'
@Emmanuel, How to use to get categories using LIKE?
it doesn't work on wordpress version 5.7, it always generates random guid like this {d258ecae8e3704c8ad532e3b46f99bdc69c2c746b3fe2b2757f5be7b108912ed}my_keyword{d258ecae8e3704c8ad532e3b46f99bdc69c2c746b3fe2b2757f5be7b108912ed}
@JSN I thought I was alone in this been trying to find out whats happening with the crazy output using % seems its 5.7? this isn't cool.
@Sam, I ended up using remove_placeholder_escape, like this $db_item = $wpdb->get_results($wpdb->remove_placeholder_escape($wpdb->prepare("SELECT * FROM wp_wowhead_items WHERE name LIKE %s", '%$text%'")))
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.