Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 4 down vote accepted

$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)

$wpdb->prepare(
        "SELECT *  FROM location  
         WHERE name LIKE %s OR name LIKE %s", 
         '%'.like_escape('on'), 
         '%'.like_escape('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.

share|improve this answer
    
very clearly, thanks you so much –  rocky Aug 15 '13 at 14:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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