Making specific table query of Wordpress?

Asked

Viewed 1,085 times

3

How to make a query in Wordpress in a specific table, being it: wp_dd_spg_galleries?

I already have the ID of the gallery, I tried to make this query, but does not return anything:

$query = "SELECT * FROM wp_dd_spg_galleries WHERE id = ".$idGaleria;
$idGal = $wpdb->query($wpdb->prepare($query));
  • I got it, doing like this: $idGal = $wpdb->get_results($query); Being illiterate with Wordpress gives it... rs

  • 1

    Ewerton, publish your solution as a complete answer, thank you!

1 answer

4


The $wpdb->query() will only return the number of affected columns or false if nothing happens then in case you should use $wpdb->get_results().

I also recommend you use {$wpdb->prefix} to get the prefix of the bank that will not always be wp_.

Besides, you used the $wpdb->prepare() the wrong way, because it works as printf() and that’s how it will escape the data.

Here is the correct way to make your query:

$idGal = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}dd_spg_galleries WHERE id = %d", $idGaleria ) );

Browser other questions tagged

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