Search in WP, "%" does not work

Asked

Viewed 248 times

0

I have a search in wordpress and she uses the following:

        $form_data = $wpdb->get_results(
            $wpdb->prepare(
                'SELECT * FROM ' . $table . ' ucf WHERE name LIKE "%'.$wpdb->esc_like($this->table_search).'%"',
                $this->form_active,
                '%' . $wpdb->esc_like($this->table_search) . '%'
            )
        , ARRAY_A);

the problem is that he does not think so, but if I put what I want to seek in this way:

        $form_data = $wpdb->get_results(
            $wpdb->prepare(
                'SELECT * FROM ' . $table . ' ucf WHERE name LIKE "%teste%"',
                $this->form_active,
                '%' . $wpdb->esc_like($this->table_search) . '%'
            )
        , ARRAY_A);

or

        $form_data = $wpdb->get_results(
            $wpdb->prepare(
                'SELECT * FROM ' . $table . ' ucf WHERE name LIKE "'.$wpdb->esc_like($this->table_search).'"',
                $this->form_active,
                '%' . $wpdb->esc_like($this->table_search) . '%'
            )
        , ARRAY_A);

and search the full text without the % % Does he think, does anyone know why ? I need a search that finds any part of the text and not only if the whole text is...

  • check the function return $wpdb->esc_like he must be coming with %

  • is not coming with % No, if I leave without the % it searches but only the whole text...

1 answer

1


I don’t think the problem is %, but the way you’re using $wpdb->prepare():

$like = '%' . $wpdb->esc_like($this->table_search) . '%';
$form_data = $wpdb->get_results(
    $wpdb->prepare(
        "SELECT * FROM $table ucf WHERE name LIKE '%s'",
        $like
    )
, ARRAY_A);

In the method prepare() the first parameter is the complete string with placeholders (%s for string, %d for int, for example, the same of sprintf()). The rest are values that must be inserted in each placeholder, in order. In your example since there was no placeholder in the string and two subsequent values, something was wrong.

  • solved! thanks!

  • if you can help me with another question, http://answall.com/questions/161119/consulta-mysql-gerando-erro-quando-uso-a-cl%C3%A1usula-or/161131? noredirect=1#comment331389_161131 is very similar and is the same code...vlw!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.