WordPress.org

Ready to get started?Download WordPress

Forums

Problem with double-% escaped in LIKE query (1 post)

  1. Derzone
    Member
    Posted 2 years ago #

    Hi there !

    I am using prepared queries, and I would like to use the LIKE argument in it.
    I read that we had to escape the % symbol.

    I already made some tests and it works perfectly with the following :
    LIKE '%s'
    -> search the exact term

    LIKE '%%%s'
    -> search before the term

    LIKE '%%%s%%'
    -> search before and after the term

    but this won't work :
    LIKE '%s%%'
    -> search after the term

    The thing is that if I try my query directly in PHP My Admin, it will work perfectly.
    So it comes from the prepared queries, and I don't know if this is a known issue or a mistake from me...

    Thank's by advance for your answers, and sorry by advance for my english :)

    [EDIT]
    Here is my SQL query, in case of...


    SELECT
    CASE
    WHEN (display_name LIKE '%s') THEN 100
    WHEN (display_name LIKE '%s%%') THEN 75
    WHEN (display_name LIKE '%%%s') THEN 50
    WHEN (display_name LIKE '%%%s%%') THEN 25
    END
    AS relevance, display_name AS valeur FROM wp_users
    WHERE (display_name LIKE '%s') OR (display_name LIKE '%s%%') OR (display_name LIKE '%%%s') OR (display_name LIKE '%%%s%%')

    ORDER BY relevance DESC
    LIMIT 0, 7",
    $search,
    $search,
    $search,
    $search,
    $search,
    $search,
    $search,
    $search

    [/EDIT]

Topic Closed

This topic has been closed to new replies.

About this Topic