• Hi,

    I noticed that I have an issue with a search method I was using and took me a while to notice that actually preparing the LIKE input string is causing an issue. I am using the following method

    $wpdb->prepare(‘%s’, ‘%blabla%’)

    which gives me something like this:

    ‘{c4b7b43cbc69ed8d327048c2314762a3bd3d02a502d3c40a16850e2fdb0a0b0b}blabla{c4b7b43cbc69ed8d327048c2314762a3bd3d02a502d3c40a16850e2fdb0a0b0b}’

    According to the documentation I am supposed to get the following: ‘%blabla%’.

    Please help.

    Thank you in advance.

    Ivan

Viewing 3 replies - 1 through 3 (of 3 total)
  • But does it work?
    If you are looking at the intermediate value, you might be seeing the hash used to preserve the % in the LIKE so it doesn’t disappear because of string manipulation using % placeholders.

    Thread Starter ivanivanovski1

    (@ivanivanovski1)

    The problem is that it works very oddly, some values work fine, some don’t.

    You should have something more like
    $wpdb->prepare( "SELECT * FROM table WHERE column = %s AND field = %d OR other_field LIKE %s", array( ‘foo’, 1337, ‘%bar’ ) );
    This example is taken from https://developer.www.ads-software.com/reference/classes/wpdb/prepare/
    where it says

    Literal percentage signs (%) in the query string must be written as %%. Percentage wildcards (for example, to use in LIKE syntax) must be passed via a substitution argument containing the complete LIKE string, these cannot be inserted directly in the query string.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Issue with wpdb->prepare’ is closed to new replies.