无法识别 Wordpress 自定义数据库表 [英] Wordpress custom database table not recognized

查看:36
本文介绍了无法识别 Wordpress 自定义数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的 wordpress (v3.4.2) 站点手动创建了一个自定义表 sdp_invites.过去我已经这样做了很多次,但现在我无法使用任何 $wpdb 方法:

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...

错误如下:

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')

我知道该表存在,因为我手动创建了它并且我可以在 phpMyAdmin 中看到它.知道为什么我会收到此错误吗?

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?

更新:WP 用户被称为root"(在我的本地开发设置中).以下是 phpMyAdmin 中列出的权限:

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    

更新2:为了排除多种可能性,我尝试了一个更简单的查询:"SELECT * FROM sdp_invites".这会引发相同的错误.当我更改为 wp 核心表时,它可以正常工作 "SELECT * FROM sdp_invites"

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"

推荐答案

我最好的猜测是你没有授予你的 Wordpress MySQL 数据库用户使用你的新自定义表的权限,大概是因为你用不同的 MySQL 用户创建了它通过 phpMyAdmin.尝试运行:

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:

GRANT ALL PRIVILEGES ON sow2.spd_invites TO 'wordpress_user'@'wordpress_host';
FLUSH PRIVILEGES; -- load new privileges

将wordpress_user"替换为您的 Wordpress MySQL 用户名(可以在 wp-config.php 中找到)并将wordpress_host"替换为您的 Wordpress 服务器的主机名(如果在同一台服务器上,则为 localhost).

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).

请记住,这将允许 Wordpress MySQL 对此表的完全权限(SELECTINSERTDROP 等),其中可能并不理想.有关 GRANT 命令的信息,请参阅 MySQL 文档 - http://dev.mysql.com/doc/refman/5.5/en/grant.html

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 the GRANT command - http://dev.mysql.com/doc/refman/5.5/en/grant.html

您需要使用 query() 函数运行插入,因为它不会返回任何结果:

You will want to run your insert using the query() function as it won't return any results:

$wpdb->query($query);

要查看您的 Wordpress MySQL 用户可见的所有表格,请将以下内容添加到您主题的 functions.php 文件中,以在每页底部打印出表格名称:

To view all tables visible to your Wordpress MySQL user, add the following to your theme's functions.php file to print out the table names at the bottom of every page:

function show_all_tables(){
    global $wpdb; 
    foreach($wpdb->get_results("SHOW TABLES", ARRAY_N) as $table): 
        echo $table[0]."<br/>"; 
    endforeach;
} 
add_action('wp_footer', 'show_all_tables');

这篇关于无法识别 Wordpress 自定义数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆