Weptile Weptile - 7 months ago 64
SQL Question

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...

Answer

Try this:

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