最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sql - $wpdb->prepare with LIKE returning blank array instead of rows

programmeradmin0浏览0评论

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 badge
Add a comment  | 

1 Answer 1

Reset to default 2

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

$prep = $wpdb->prepare( "SELECT * FROM {$table_name} WHERE name LIKE '%s'", $city );
发布评论

评论列表(0)

  1. 暂无评论