Query String wrong with use of prepare. Wordpress

Asked

Viewed 35 times

1

I have a problem building a query here:

$sql = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}imagens WHERE categorias LIKE %d limit %s, %s", array('%' . $wpdb->esc_like($var['busca']) . '%', $init, 45));

$registers = $wpdb->get_results($sql);

Returns nothing.

Before it was like this and I listed it perfectly:

$sql = "SELECT * FROM wp_imagens WHERE categorias LIKE '%".$var['busca']."%' $init,45";

$registers = $wpdb->get_results($sql);

What’s wrong with the prepare query?

  • With prepare do not need a execute no? Type: $wpdb->execute();

2 answers

0

You used the placeholder %d for a string parameter, where it is for integers. Use placeholder %s.

$sql = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}imagens WHERE categorias LIKE %s limit %s, %s", array('%' . $wpdb->esc_like($var['busca']) . '%', $init, 45));

$registers = $wpdb->get_results($sql);

0

I think you should run the query before getting the results:

$sql = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}imagens WHERE categorias LIKE %d limit %s, %s", array('%' . $wpdb->esc_like($var['busca']) . '%', $init, 45));
$sql->execute();
$registers = $sql->get_results();

Browser other questions tagged

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