dongwo7858 2014-06-04 10:15
浏览 76
已采纳

sql LIKE查询只返回完全匹配,而不是类似的匹配

I'm working on a $wpdb query to search for items in the media library. The search kind of works, except it only returns exact matches.

I have two images in the library, one called run and the other called running, if I search just run, it only returns the exact match, running isn't returned. Am I missing something here? I thought LIKE returned things that contains those characters?

Here's my query in full, I'm using AJAX to stick it into the page.

include_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php' );

global $wpdb;

if( isset($_GET['nameLike']) ) {
    $search = $_GET['nameLike'];
} else {
    $search = '';
}

$results = $wpdb->get_results( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_status = 'inherit' AND post_title LIKE %s", $search), ARRAY_A);

foreach ($results as $result) : ?>

    <?php $image = wp_get_attachment_image( $result[ID], array(200, 150) ); ?>

    <div class="grid-1-4"><?php echo($image); ?></div>

<?php endforeach ?>
  • 写回答

1条回答 默认 最新

  • dongwei7245 2014-06-04 10:38
    关注

    I wrote answer in the comment, I'll write here also how to implement it properly in WP. You can add '%' in your search term.

    $results = $wpdb->get_results( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_status = 'inherit' AND post_title LIKE %s", '%' . like_escape($search) . '%'), ARRAY_A);
    

    EDIT: Taken WP example from here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号