Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I know it should work with custom table and I think it worked in previous development I believe. But this time I don't know whey it is not getting custom table with $wpdb->tablename

Code:

global $wpdb;    
$items = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->review_media"));

This is not giving any result but when I tried with `$wpdb->prefix than it works

$wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM ".$wpdb->prefix."review_media"));

Any idea

share|improve this question

2 Answers 2

up vote 4 down vote accepted

$wpdb->tablename works only with the tables initially present in a WordPress installation. These tables are defined in wp-includes/wp-db.php.

The second method you used is perfectly acceptable.

share|improve this answer
    
Oh I see. I thought $wpdb-> will add prefix with custom table as well. Thanks to clarify this was new to me. –  pixelngrain Feb 23 '13 at 17:09
    
@pixelngrain, you can add your table name like that. It does work. I tried it, but I don't know if it will get you trouble later on. –  s_ha_dum Feb 23 '13 at 17:15
    
Yeh that is why I want to use prfix dynamically as I am developing plugin and prefix can be changed, depending on the installation. Thanks a lot again and using my second method. –  pixelngrain Feb 23 '13 at 17:27

You are supposed to store the table name in $wpdb before you can use it the way you did.

$wpdb->review_media = "{$wpdb->prefix}review_media";
share|improve this answer
    
Do I need to replace $tableprefix with my prefix? or that is the class variable? Because I have added this before use but still happens nothing. –  pixelngrain Feb 23 '13 at 17:07
1  
Yes, you would need to fill this variable yourself. But you can get the prefix out of $wpdb->prefix. –  s1lv3r Feb 23 '13 at 17:16
    
Oh I see. Thanks a lot for your help –  pixelngrain Feb 23 '13 at 17:26
    
Oh yeah this is great solution now.. thanks –  pixelngrain Feb 23 '13 at 17:38
    
Thank's for editing kaiser. Should have been that simple in the first place. :-) –  s1lv3r Feb 23 '13 at 18:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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