1

i use a like %text% statement in wordpress:

SELECT *  FROM location  WHERE name LIKE "%on" OR name LIKE "%on%"

i tried anything but it's not run:

$wpdb->get_results($wpdb -> prepare("SELECT *  FROM location  WHERE name LIKE %1s OR name LIKE %2s", '%'.like_escape('on'), '%'.like_escape('on').'%'), ARRAY_A);

please help me

1 Answer 1

5

$wpdb->prepare() doesn't actually fully support sprintf() placeholders.

From the Codex:

The query parameter for prepare accepts sprintf()-like placeholders. The %s (string), %d (integer) and %f (float) formats are supported.

It's not entirely obvious from that but the argument swapping formats (e.g. %1$d) are not supported (in any case you have the incorrect syntax: it should be %2$s instead of %2s)

global $wpdb;
$wpdb->prepare(
        "SELECT *  FROM location  
         WHERE name LIKE %s OR name LIKE %s", 
         '%'.$wpdb->esc_like('on'), 
         '%'.$wpdb->esc_like('on').'%'
), ARRAY_A);

I'm assuming 'on' might be replaced by a unknown value - otherwise $wpdb->prepare() isn't necessary. On an unrelated note, the second condition in your SQL makes the first obsolete.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.