I'm trying to pull some custom data from a Cart66 table.

My code works whenever I pull results from standard WP table in (my array is populated and the print_r shows that data), but not with any Cart66 tables (it prints an empty array).

I am absolutely certain that I've got data in these tables (I've rechecked that I'm using the correct table names about 1,000,000 times). I've tried two Cart66 tables which have data in them.

The codex says the get_results class should work with any table, not just standard tables. I thought it might be the underscore that was causing trouble, but underscores are supposed to be fine, and indeed, standard WP tables with underscores work fine.

$rawproducts = $wpdb->get_results( "SELECT * FROM $wpdb->cart66_products" );

This is maddening! Any ideas?


  • 1
    What is the ouput for the print_r of $rawproducts? Also, just concatenate strings for clarity: 'SELECT * FROM '.$wpdb->cart66_products.
    – Geert
    May 2, 2012 at 13:29
  • $wpdb->cart66_products is probably not a property of $wpdb. I'm not familiar with Cart66, but it looks like they probably know what they're doing so I'm assuming they prepended the current WordPress prefix to the table name. If that's the case, you should always use that when referencing the table as in {$wpdb->prefix}my_table_name - note you don't need an underscore, because that's part of the prefix.
    – Tom Auger
    May 2, 2012 at 13:50
  • Does Cart66 not include any extensions to WP_Query that you can use, rather than having to manually create the SQL? It's a pretty big plugin - I would imagine they have some API for developers like you. See if you can stay out of the SQL as much as possible.
    – Tom Auger
    May 2, 2012 at 13:57
  • Geert: As I mentioned, the output is an empty array - "Array()".
    – jfacemyer
    May 2, 2012 at 14:45
  • Tom: this helps me get to an answer. I misunderstood the codex. When I used just the table name, it worked fine (without $wpdb->). I'm still working out the syntax of {$wpdb->prefix}table_name. See below on fischi's answer.
    – jfacemyer
    May 2, 2012 at 14:51

3 Answers 3


you do not have to specify the $wpdb again in your query, but be sure that your table uses the prefix, if it has one. if it doesn't, skip the part with the prefix. also, you should always prepare a manually added query first. this should do it:

global $wpdb;
$query = $wpdb->prepare( "SELECT * FROM %s", "{$wpdb->prefix}cart66_products" );
$rawproducts = $wpdb->get_results( $query );
  • 2
    prepare() is pretty much the same as sprintf. You have to use %s for string and %d for digit replacements. Else it helps nothing. It would be the same as sprintf( "hello $bad_code" );.
    – kaiser
    May 2, 2012 at 14:24
  • 1
    This didn't work out of the box for me. I got it to work by putting in the whole table name only and not using $wpdb->table, but I'd like this to work so I can have a portable plugin that's set up correctly.
    – jfacemyer
    May 2, 2012 at 14:56
  • @fischi OK, so this works fine: ` $thisquery = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}cart66_products" ); $rawproducts = $wpdb->get_results( $thisquery ); print_r($rawproducts);` but not this: ` $thisquery = $wpdb->prepare( "SELECT * FROM %s", "{$wpdb->prefix}cart66_products" ); $rawproducts = $wpdb->get_results( $thisquery ); print_r($rawproducts);`
    – jfacemyer
    May 2, 2012 at 15:21
  • @jfacemyer You have to strip white spaces from before and after code to get code highlighting in comments. Also: "didn´t work" is a no-go. You´ll have to provide full error statements. Else it´s guessing.
    – kaiser
    May 2, 2012 at 16:01
  • @kaiser thank you, of course you are right here. i was under the impression prepare() also checks for bad characters. so, in conclusion, if you do not enter user input in your query, the prepare() is not really necessary?
    – fischi
    May 2, 2012 at 16:21
global $wpdb;
$query = $wpdb->prepare( 
        SELECT * FROM %s
$results = $wpdb->get_results( $query );

echo '<pre>';
    // Show results
    echo "<hr />Results:\n";
    print_r( $results );

    // Show errors
    echo "<hr />Errors:\n";
    if ( current_user_can( 'manage_options' ) AND defined( 'WP_DEBUG' ) AND WP_DEBUG AND defined( 'WP_DEBUG_DISPLAY' ) AND WP_DEBUG_DISPLAY )
echo '</pre>';

// Savely remove errors for guest or non admins

More info about error handling inside WPDB can be found here in Codex.

global $wpdb;
$table_name = $wpdb->prefix . 'cart66_products';
$i = 0;
$obj = [];

$query = $wpdb->prepare(
    "SELECT * FROM $table_name"

$result = $wpdb->get_results(
foreach ($result as $row) {
    $obj[$i] = $row;
    echo '<pre>';
    echo "<hr />Results:\n";
    echo json_encode($obj);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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