I trying to select data from a table using LIKE and wildcards. I tried basically everything. Here'is the last code according to WordPress documentation:


global $wpdb;

$table_name = $wpdb->prefix . 'city';

$city= 'New York';
$city = $wpdb->esc_like( $city );
$city = '%' . $city . '%';

$prep = $wpdb->prepare("
    SELECT * FROM {$table_name} LIKE name=%s
    ", $city);

echo $prep . '<br><br>';

$rowsSelected = $wpdb->query($prep);



The output:

SELECT * FROM wp_city LIKE name='{7fd8d56e635a959f67faccf3bbff451d20c9e3acfb7bb1a69dc35208e9a29109}New York{7fd8d56e635a959f67faccf3bbff451d20c9e3acfb7bb1a69dc35208e9a29109}'

Array ( )

The issue: I always get an empty array

WordPress version: 5.2.2


Your SQL doesn't look right. It should be more like

$prep = $wpdb->prepare( "SELECT * FROM {$table_name} WHERE name LIKE '%s'", $city );
  • 1
    I mixed things up and looking to wrong things. Thanks so much! – NoWay Aug 27 '19 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.