I’ve manually created a custom table, sdp_invites, for my wordpress (v3.4.2) site. I’ve done this many times in the past, but now I’m unable to use any of the $wpdb methods:
global $wpdb;
global $current_user;
$query = "INSERT INTO spd_invites (sender_id,receiver_id,job_id,job_title,job_slug,employer_id) VALUES ('{$current_user->ID}','{$resume->user_id}','{$job_id}','{$job_title}','{$job_slug}','{$employer_id}')";
$wpdb->get_results($query); //ERROR...
$wpdb->query($query); //ERROR...
Here’s the error:
WordPress database error: [Table 'sow2.spd_invites' doesn't exist]
INSERT INTO spd_invites (sender_id,receiver_id,job_id,job_title,job_slug,employer_id) VALUES ('799','809','6','Professional Basket Weaver','professional-basket-weaver','5')
I know the table exists because I manually created it and I can see it in phpMyAdmin. Any idea why I’m getting this error?
UPDATE:
The WP user is called “root” (on my local dev setup). Here’s the privileges listed in phpMyAdmin:
User Host Type Privileges Grant Action
root localhost global ALL PRIVILEGES Yes
UPDATE2:
Just to rule out a bunch of possibilities, I tried a simpler query: "SELECT * FROM sdp_invites". This throws the same error. When I change to a wp core table, it works without error "SELECT * FROM sdp_invites"
My best guess is that you haven’t granted permission to you WordPress MySQL database user for your new custom table, presumably because you created it with a different MySQL user through
phpMyAdmin. Try running:Replace “wordpress_user” with your WordPress MySQL username (can be found in
wp-config.php) and “wordpress_host” with the hostname of your WordPress server (or localhost if it’s on the same server).Keep in mind that this will allow the WordPress MySQL full permissions (
SELECT,INSERT,DROP, etc) to this table, which may not be desirable. See the MySQL docs for my info on theGRANTcommand – http://dev.mysql.com/doc/refman/5.5/en/grant.htmlYou will want to run your insert using the
query()function as it won’t return any results:To view all tables visible to your WordPress MySQL user, add the following to your theme’s
functions.phpfile to print out the table names at the bottom of every page: