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:
<?php
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);
print_r($wpdb->last_result);
$wpdb->flush();
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
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:
<?php
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);
print_r($wpdb->last_result);
$wpdb->flush();
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
Share Improve this question asked Aug 27, 2019 at 14:17 NoWayNoWay 31 bronze badge1 Answer
Reset to default 2Your SQL doesn't look right. It should be more like
$prep = $wpdb->prepare( "SELECT * FROM {$table_name} WHERE name LIKE '%s'", $city );