Support » Plugins » Hacks » Simple $wpdb-> question

  • I’m trying to create an INSERT statement for a custom database table I’ve made. Everything I’ve read suggests:
    $wpdb->insert( $wpdb->custom_table, array( 'val1' => $newvalueone,'val2' => $newvaluetwo ) );

    Am I supposed to declare or initialize my custom table somewhere? I’m not creating a plugin, I’m just creating a custom function (in functions.php) for me.

    I can get all my SQL queries to work if I change $wpdb->custom_table into wp_custom_table, but I know that’s not the correct method.

    I get this error if I do $wpdb->custom_table:

    WordPress database error: [Incorrect table name '']
    INSERT INTO <code></code> (<code>val1</code>,<code>val2</code>) VALUES ('Hello World!','')

    I saw one site that has this code:
    $wpdb->custom_table= $wpdb->prefix.'custom_table';
    but it only works once, within that function. Am I supposed to globally declare it somewhere?

    Thank you,

Viewing 13 replies - 1 through 13 (of 13 total)
  • don’t forget to initialize the $wpdb as a global variable…

    That’s not actually making a table, all it’s doing is storing the -name- of the table in the class.

    You don’t seem to have too much experience with SQL syntax, perhaps you should bone up on a primer. Here’s a good one —

    Have you actually made the table in the database yet? And the previous poster is right … you need to always always always declare $wpdb to be global before doing anything with it, whether it’s saving a value in it or doing operations.


    Thanks for the replies. I created the table in mySQL (wp_custom_table).
    I made several fields & added sample data.

    I know how to work with it in php, but am trying to learn WP’s code.

    I’m editing my theme’s functions.php file.

    My functions look like this:

    function wpTestFnctn(){
    global $wpdb;
    $coinfo = $wpdb->get_row($wpdb->prepare("SELECT * FROM wp_custom_table WHERE app=1"));

    I know it’s supposed to have $wpdb->custom_table in the SELECT statement, but it doesn’t work if I do that. It only works if I hard code in the table prefix ‘wp_’, so I feel as though there’s something I’m overlooking.

    The same goes with my INSERT
    $wpdb->insert( $wpdb->custom_table, array( 'val1' => $newvalueone,'val2' => $newvaluetwo ) );
    If I change it to
    $wpdb->insert( wp_custom_table, array( 'val1' => $newvalueone,'val2' => $newvaluetwo ) );
    It works.

    I have global $wpdb; at the top of the function. If I add this line next, it works, but I don’t think I’m supposed to do this every time
    $wpdb->dcdirectory = $wpdb->prefix.'custom_table';

    Did you at any point store the name of your custom table in $wpdb?

    Try running something earlier that does, in essence …

    function on_mytheme_init(){
        global $wpdb;
        $wpdb->custom_table = $wpdb->prefix . 'custom_table';

    In essence, you’re trying to get a value from $wpdb that was never there!

    That’s what I’ve been looking for. Worked. Thank you

    I don’t understand what this line is about.

    $wpdb->custom_table = $wpdb->prefix . 'custom_table';

    I just got a $wpdb->insert to insert fine into a custom table which does not have the prefix, without having to include such a definition. The global $wpdb line has to be there, but not that line.

    Alright, Bill, here’s the bit that you’re missing …

    The table name.

    I’m guessing that you’re just entering the table name you want to use in the query manually. What Crowd wants to do (or is trying to do, whether or not he wants to) is store the table name as a member-variable of the $wpdb object.

    Kinda like how we can just write inserts like INSERT INTO $wpdb->users SET name = 'myname' , password = MD5('mypassword') etc. It lets you store the full table name in the $wpdb object, so you don’t have to worry about appending table_prefix to the name each time you use it.

    Make sense?

    I’ve read what I believe is quite a bit on how to interact with databases using WP (I want to use WP functions), and everytime I’ve tried to access the database with the built in $wpdb-> commands, my queries fail. If I simply replace the $wpdb->tablename part with wp_tablename, it would work.

    So I assumed there was some ‘declaration’ I was missing. However, I could never find any documentation on what to do.

    I’m no programming ace, but I’m trying to find the correct method to access a table I created.

    So Bill, this line:
    $wpdb->custom_table = $wpdb->prefix . 'custom_table';
    made $wpdb->custom_table work when placed within an SQL query.

    George, what do normal programmers do to access a custom table? Is this not correct? (I only do this with an add_filter/init line).

    If you’re building modular code, you have to take into consideration that the table prefix CAN be something other than wp_ — if a user changes their installation to run off foo_ for the prefix, your code will either die horribly, or not fall within wp coding standards using the proper prefix.

    For me, you can look at my plugin, Ndizi Project Management to see how I do it. I just store the table names in my own class, rather than the WPDB class, but the principle is the same.

    Thanks for the info, George.

    Thanks for the clarification.

    So if I get what you’re saying, in order to write modular code for plugins and such you want to use whatever table prefix is used for the rest of the WP install, and by doing so you can define the table name using

    $wpdb->custom_table = $wpdb->prefix . 'custom_table';

    and then refer to the table using the form

    $wpdb->insert( $wpdb->custom_table, ...

    within your code. The first line simply translates $wpdb->custom_table to $wpdb->prefix . ‘custom_table’ and you don’t have to insert the prefix every time you make a call.

    In my case, I’m writing code for a single WP install, so I can call the table whatever I wish, with or without the prefix, and call it directly without it bombing.


    Unless, of course, you’re dealing with a multiblog install.

    But in essence, yup. Call it whatever you like.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Simple $wpdb-> question’ is closed to new replies.