Title: Simple $wpdb-&gt; question
Last modified: August 19, 2016

---

# Simple $wpdb-> question

 *  [TheDailyCrowdsource](https://wordpress.org/support/users/thedailycrowdsource/)
 * (@thedailycrowdsource)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/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!','http://google.com')
       ```
   
 * 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,
    -David

Viewing 13 replies - 1 through 13 (of 13 total)

 *  [Joseph G.](https://wordpress.org/support/users/dunhakdis/)
 * (@dunhakdis)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-wpdb-question/#post-1825952)
 * don’t forget to initialize the $wpdb as a global variable…
 *  [George Stephanis](https://wordpress.org/support/users/georgestephanis/)
 * (@georgestephanis)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-wpdb-question/#post-1825973)
 * 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 — [http://www.w3schools.com/web/web_sql.asp](http://www.w3schools.com/web/web_sql.asp)
 * 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.
 *  Thread Starter [TheDailyCrowdsource](https://wordpress.org/support/users/thedailycrowdsource/)
 * (@thedailycrowdsource)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-wpdb-question/#post-1826015)
 * Hi,
 * 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.
 *  Thread Starter [TheDailyCrowdsource](https://wordpress.org/support/users/thedailycrowdsource/)
 * (@thedailycrowdsource)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-wpdb-question/#post-1826016)
 * 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';`
 *  [George Stephanis](https://wordpress.org/support/users/georgestephanis/)
 * (@georgestephanis)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-wpdb-question/#post-1826017)
 * 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';
       }
       ```
   
 * [http://codex.wordpress.org/Creating_Tables_with_Plugins#The_Whole_Function](http://codex.wordpress.org/Creating_Tables_with_Plugins#The_Whole_Function)
 * In essence, you’re trying to get a value from $wpdb that was never there!
 *  Thread Starter [TheDailyCrowdsource](https://wordpress.org/support/users/thedailycrowdsource/)
 * (@thedailycrowdsource)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-wpdb-question/#post-1826018)
 * That’s what I’ve been looking for. Worked. Thank you
 *  [billc108](https://wordpress.org/support/users/billc108/)
 * (@billc108)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-wpdb-question/#post-1826023)
 * 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.
 *  [George Stephanis](https://wordpress.org/support/users/georgestephanis/)
 * (@georgestephanis)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-wpdb-question/#post-1826024)
 * 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?
 *  Thread Starter [TheDailyCrowdsource](https://wordpress.org/support/users/thedailycrowdsource/)
 * (@thedailycrowdsource)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-wpdb-question/#post-1826025)
 * 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).
 *  [George Stephanis](https://wordpress.org/support/users/georgestephanis/)
 * (@georgestephanis)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-wpdb-question/#post-1826026)
 * 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.
 *  Thread Starter [TheDailyCrowdsource](https://wordpress.org/support/users/thedailycrowdsource/)
 * (@thedailycrowdsource)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-wpdb-question/#post-1826027)
 * Thanks for the info, George.
 *  [billc108](https://wordpress.org/support/users/billc108/)
 * (@billc108)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-wpdb-question/#post-1826029)
 * 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.
 *  [George Stephanis](https://wordpress.org/support/users/georgestephanis/)
 * (@georgestephanis)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-wpdb-question/#post-1826030)
 * Yup!
 * 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.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 13 replies
 * 4 participants
 * Last reply from: [George Stephanis](https://wordpress.org/support/users/georgestephanis/)
 * Last activity: [15 years, 4 months ago](https://wordpress.org/support/topic/simple-wpdb-question/#post-1826030)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
