4

I am quite new in WP Plugin World. I am trying to develop a plugin in WP 3.1 when I am trying to insert data into my table named "wp_enam" in the following way:

$wpdb->insert($wpdb->enam, array('username' => "enam" ,
                                 'useremail' => "myemail@somedomain.com"));

it is not working.

I try to debug it in following way:

$wpdb->show_errors();
$wpdb->insert($wpdb->enam, array('username' => "enam" ,
                                 'useremail' => "myemail@somedomain.com"));
$wpdb->print_error(); 

Now I am getting following message from MR.WP

WordPress database error: [Incorrect table name '']
INSERT INTO `` (`username`,`useremail`) VALUES ('enam','myemail@somedomain.com')

WordPress database error: [Incorrect table name '']
INSERT INTO `` (`username`,`useremail`) VALUES ('enam','myemail@somedomain.com')

As you can see the table name is not showing in the mysql query. Is this a correct way to access a table name with $wpdb->my_table? I am using mysql. Thanks in advance.

Edit 1: Looks like $wpdb->tblnamedo not add the table prefix anymore! As per "Professional WordPress Wrox" by Hal Stern, David Damstra and Brad Williams" (which is a great book ) it should work. The above functionality is explained at this book in the following way:

$wpdb->my_custom_table to reference the table in WordPress. 
This translates to wp_my_custom_table if wp_ is the table prefix. 
This is the proper way to determine the correct table prefix when working with tables in the WordPress database.

(Page:107)

2
  • Huh. I don't think that ever worked. Probably worth submitting an errata report, although the newer book "Professional WordPress Plugin Development" doesn't repeat that error, so it may have already been caught. Mar 28, 2011 at 19:35
  • @goldenapples Thanks for your reply. I will collect the newer one of that book.
    – enam
    Mar 29, 2011 at 5:46

3 Answers 3

5

You can check database function for database here. For the table prefix matter you should use $wpdb->prefix . 'enam' and it will return the table prefix. Just add the table name with this. So the total code would be :

$yourtablename =  $wpdb->prefix . 'enam';

so your total code could be something like:

$wpdb->insert($yourtablename , array('username' => "enam" ,
                             'useremail' => "myemail@somedomain.com"));

EDIT: If you need more information you can see THIS article. This is very useful article for creating plugin with database.

2
  • what is the book name , anyway?
    – user4254
    Mar 27, 2011 at 19:22
  • 1
    The book name is: "Professional WordPress Wrox" by Hal Stern, David Damstra and Brad Williams. It is really a nice book, though I just stack into the inserting data into db part(it actually wrote for WP2.8.X, I guess)
    – enam
    Mar 27, 2011 at 19:28
3

You should use something like

$wpdb->prefix . 'table_name'

instead.

If you don't wan't to repeat your table name through your code save it in a variable or create a function like this one: (nothing cool, but I use it this way, works if you have just one custom table, so it is not very versatile..)

function get_table_name() {
    global $wpdb;
    return $wpdb->prefix . 'table_name';
}
4
  • +1 well, nice idea. Actually, I find '$wpdb->my_table' this in a book which is written for WP2.8.X. Is this not legal in WP3.1?
    – enam
    Mar 27, 2011 at 18:49
  • 4
    I set the table name as a property: $wpdb->enam = $wpdb->base_prefix . 'enam';. Now you can use $wpdb->enam as you were! Mar 27, 2011 at 18:54
  • @TheDeadMedic wOw nice idea. But, do you have any idea why $wpdb->enam is not working in my code for WP3.1? Because, as per that book it should add the table prefix with the table name(as you do here!)?
    – enam
    Mar 27, 2011 at 19:06
  • Place this as the first line of your plugin's code, and you'll be able to use it anytime after that; $GLOBALS['wpdb']->enam = $GLOBALS['wpdb']->base_prefix . 'enam'; Mar 28, 2011 at 23:51
0

I think the wpdb->enam is incorrect, it doesnt exist , you just have to enter tablename there.

$wpdb->insert("table_name", array('username' => "enam" ,
                             'useremail' => "myemail@somedomain.com"));
2
  • as per codex.wordpress.org/Function_Reference/wpdb_Class $wpdb->tablename is legal....:|
    – enam
    Mar 27, 2011 at 19:13
  • $wpdb->tablename is only valid for tablenames that are registered as properties of the $wpdb class. You can see how the default tables are registered in wp-includes/wp-dp.php - its basically the same way as @TheDeadMedic's answer above. Mar 28, 2011 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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