WordPress php mysql LIKE query not working

$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…

Related posts

Leave a Reply

3 comments

  1. Escape it with double %%

    When using get_results() in WordPress, all % characters inside SQL string literals, including LIKE wildcards, must be double-% escaped as %%

    $db_item = $wpdb->get_results($wpdb->prepare(

    "SELECT * FROM wp_wowhead_items WHERE name LIKE '%%$text%%'"
    

    ));