Query Custom WordPress/buddyPress Table (PHP/MySQL)

458 views Asked by At

I have been looking around and trying different things for a while and no luck.

Scenario: I have WordPress/BuddyPress and I added in a few new tables to the database through phpMyAdmin. I cannot run a successful query to them, yet I can to all the original tables. I have tried many things, this is my most recent try, still not working:

$b1_exc = $wpdb->get_results( $wpdb->prepare("SELECT * FROM memberbadge
WHERE 1") );

I would really appreciate a solution to add custom tables and be able to query them.

Thank you in advance!

2

There are 2 answers

1
shanebp On BEST ANSWER

It's probably a prefix problem. Try:

$wpdb->get_results( "SELECT * FROM {$wpdb->prefix}memberbadge") );

If you really need a WHERE clause and the variable comes from user input, then use prepare.

0
Paresh Radadiya On

As of 3.5, wpdb::prepare() enforces a minimum of 2 arguments.

This is the correct syntax for wpdb::prepare(), use this.

$result = $wpdb->get_results( $wpdb->prepare("SELECT * FROM memberbadge WHERE %d", 1) );